preprocess the Strings by storing for each character of every String , the index of their occurrence we will use a common list for all because of only the index matter in the String from which the character was picked
List < int > [ ] index = new List < int > [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) index [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < a [ i ] . Length ; j ++ ) {