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
let index = [ ] ; for ( let i = 0 ; i < 26 ; i ++ ) index . push ( [ ] ) ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < a [ i ] . length ; j ++ ) {