
Let the list of words be {cod, zcz} and the pattern be ‘nin’.
For each word in the list, we will check whether the word matches the pattern or not:
For the word ‘cod’:
Letter ‘n’ in the pattern maps to letter ‘c’ in the word.
Letter ‘i’ in the pattern maps to letter ‘o’ in the word.
Letter ‘n’ in the pattern maps to letter ‘d’ in the word.
As the same letter ‘n’ in the pattern, maps to two different letters ‘c’ and ‘d’ in the word. Hence, ‘cod’ is not a valid match.
For the word 'zcz':
Letter ‘n’ in the pattern maps to letter ‘z’ in the word.
Letter ‘i’ in the pattern maps to letter ‘c’ in the word.
Letter ‘n’ in the pattern maps to letter ‘z’ in the word.
As every letter in the pattern maps uniquely to a corresponding letter in the word. Hence 'zcz' is a valid match.
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains an integer ‘N’ denoting the number of words present in the list.
The second line of every test case contains ‘N’ space-separated strings denoting the words in the list.
The third line of every test case contains the pattern string.
For each test case, print space-separated words which are a valid match with the given pattern.
Print the words in the same order in which they occur in the list.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10 ^ 2
1 <= Length of pattern, Length of each word <= 10 ^ 3
Time Limit: 1 sec
The idea is to generate hashes of each word present in the list and compare this hash value with the hash of the pattern string.
How do we generate a hash for a given word or pattern?
Let the word be “aabaddeeecf” and the pattern be “ppqpsstttvu”.
Generating the string of integers (hash) for the word:
a -> 1
b -> 2
d -> 3
e -> 4
c -> 5
f -> 6
Hash for the word: 11213344456
Generating the string of integers (hash) for the pattern:
p -> 1
q -> 2
s -> 3
t -> 4
v -> 5
u -> 6
Hash for the pattern: 11213344456
As hashes for word and pattern match each other. Hence, the word is a valid match.
Instead of generating a hash for every word and comparing it with the hash of pattern, a better approach is to map the letters of pattern and the corresponding letters in the word, to each other.
Then, check whether every character in the pattern maps uniquely to a character in a word. And also check whether every character in the word maps uniquely to a character in the pattern.
During the process of mapping three cases may arise:
The algorithm for the approach is as follows:
Let’s assume the list of words is {abb, pqr} and the pattern given is “zxx”.
Mapping the pattern and the word “abb”:
z <-> a
x <-> b
x <-> b
As every letter in the pattern maps uniquely to a corresponding letter in the word. Hence “abb” is a valid match.
Mapping the pattern and the word “pqr”:
z <-> p
x <-> q
x <-> r
As the same letter ‘x’, in the pattern is getting mapped to two different letters ‘q’ and ‘r’ in the word. Hence “pqr” is not a valid match.