


The strings are non-empty.
The strings only contain lowercase English letters.
The first line of input contains a single integer T, representing the number of test cases or queries to be run. Then the T test cases follow.
The first line of each test case contains the string pattern and an integer N, denoting the number of words in the collection.
The second line of each test case contains N-words.
For each test case print in a new line, “True” if the strings in the words array follow the same order as the order of the characters in the pattern string. Otherwise, print “False”.
You do not need to print anything, it has already been taken care of. Just implement the given function.
Output for each test case will be printed in a separate line.
1 <= T <= 100
1 <= |pattern| <= 5000,
1 <= N <= 5000
1 <= X <= 6
Time Limit: 1sec
The approach is to use hashing. We can maintain two HashMaps. One HashMap can be used to know which character corresponds to which word. The other HashMap can be used to know whether a particular word has already been matched with any other character or not.
So, for each character in the pattern, we check for its corresponding word in the first map. If the character is not present in the first map, then we check whether the current word is present in the second map If it is present, that means this word corresponds to any other character in the pattern. So, we instantly return false in that case. If the word is not present in the second map, then we add this word to the second map and make firstmap[char] = currentWord.
Otherwise, if the character is already present in the first map, then we check whether firstMap[char] equalscurrentWord. If they are equal, then continue in the loop, else return false.
The approach is to use a single HashMap. This HashMap can be used to store the index of the first occurrence of each character in the pattern and the index of the first occurrence of every unique string in the “words” array. If the current character is not present in the HashMap already, that means that the current index is the first occurrence of this character. Similarly, we can store the index of the first occurrence of every string. If the indices of the first occurrences of the present character and the present string do not match we can return False. If there is no mismatch, we can return True.
Note that, since in languages like c++, there is a difference between characters and strings, so we need to convert each character in the “pattern” to a string. It may also happen that the word in the “words” array can consist of only one character. In that case, a collision occurs and the algorithm might give incorrect results. For example, if the pattern is “abab” and “words” array is {“bat”, “a”, “bat”, “a”}. Then, this will give the wrong output. So, we add an extra character ‘_’ to each word in the “words” array to overcome this issue.