Find all strings that match specific pattern in a dictionary
Given a dictionary of words, find all strings that match the given pattern where every character in the pattern is uniquely mapped to a character in the dictionary.
Become a success story instead of just reading about them. Prepare for coding interviews at Amazon and other top product-based companies with our Amazon Test Series. Includes topic-wise practice questions on all important DSA topics along with 10 practice contests of 2 hours each. Designed by industry experts that will surely help you practice and sharpen your programming skills. Wait no more, start your preparation today!
Input: dict = ["abb", "abc", "xyz", "xyy"]; pattern = "foo" Output: [xyy abb] xyy and abb have same character at index 1 and 2 like the pattern Input: dict = ["abb", "abc", "xyz", "xyy"]; pat = "mno" Output: [abc xyz] abc and xyz have all distinct characters, similar to the pattern. Input: dict = ["abb", "abc", "xyz", "xyy"]; pattern = "aba" Output:  Pattern has same character at index 0 and 2. No word in dictionary follows the pattern. Input: dict = ["abab", "aba", "xyz", "xyx"]; pattern = "aba" Output: [aba xyx] aba and xyx have same character at index 0 and 2 like the pattern
Approach: The aim is to find whether the word has the same structure as the pattern. An approach to this problem can be to make a hash of the word and pattern and compare if they are equal or not. In simple language, we assign different integers to the distinct characters of the word and make a string of integers (hash of the word) according to the occurrence of a particular character in that word and then compare it with the hash of the pattern.
Word='xxyzzaabcdd' Pattern='mmnoopplfmm' For word-: map['x']=1; map['y']=2; map['z']=3; map['a']=4; map['b']=5; map['c']=6; map['d']=7; Hash for Word="11233445677" For Pattern-: map['m']=1; map['n']=2; map['o']=3; map['p']=4; map['l']=5; map['f']=6; Hash for Pattern="11233445611" Therefore in the given example Hash of word is not equal to Hash of pattern so this word is not included in the answer
- Encode the pattern according to the above approach and store the corresponding hash of the pattern in a string variable hash.
- Algorithm to encode -:
- Initialize a counter i=0 which will map distinct character with distinct integers.
- Read the string and if the current character is not mapped to an integer, map it to the counter value and increment it.
- Concatenate the integer mapped to the current character to the hash string.
- Now read each word and make a hash of it using the same algorithm.
- If the hash of the current word is equal to the hash of the pattern then that word is included in the final answer.
int i=0 Declare map for character in pattern: if(map[character]==map.end()) map[character]=i++; hash_pattern+=to_string(mp[character]) for words in dictionary: i=0; Declare map if(words.length==pattern.length) for character in words: if(map[character]==map.end()) map[character]=i++ hash_word+=to_string(map[character) if(hash_word==hash_pattern) print words
- Time Complexity: O(N*K).
Here ‘N’ is the number of words and ‘K’ is its length. As we have to traverse each word separately to create its hash.
- Auxiliary Space: O(N).
The use of hash_map data structure for mapping characters takes this amount of space.
Approach: Now let’s discuss a little more conceptual approach which is an even better application of maps. Instead of making a hash for each word we can map the letters of the pattern itself with the corresponding letter of the word. In case the current character has not been mapped, map it to the corresponding character of the word and if it has already been mapped, then check whether the value with which it was mapped earlier is the same as the current value of the word or not. The example below will make things easy to understand.
Word='xxyzzaa' Pattern='mmnoopp' Step 1-: map['m'] = x Step 2-: 'm' is already mapped to some value, check whether that value is equal to current character of word-:YES ('m' is mapped to x). Step 3-: map['n'] = y Step 4-: map['o'] = z Step 5-: 'o' is already mapped to some value, check whether that value is equal to current character of word-:YES ('o' is mapped to z). Step 6-: map['p'] = a Step 7-: 'p' is already mapped to some value, check whether that value is equal to current character of word-: YES ('p' is mapped to a). No contradiction so current word matches the pattern
- Create a character array in which we can map the characters of patterns with a corresponding character of a word.
- Firstly check whether the length of word and pattern is equal or not, if no then check the next word.
- If the length is equal, traverse the pattern and if the current character of the pattern has not been mapped yet, map it to the corresponding character of the word.
- If the current character is mapped, then check whether the character with which it has been mapped is equal to the current character of the word.
- If no then the word does not follow the given pattern.
- If the word follows the pattern until the last character then print the word.
for words in dictionary: char arr_map=0 if(words.length==pattern.length) for character in pattern: if(arr_map[character]==0) arr_map[character]=word[character] else if(arr_map[character]!=word[character] break the loop If above loop runs successfully Print(words)
- Time Complexity: O(N*K), where ‘N’ is the number of words and ‘K’ is its length.
To traverse each word, this will be the time requirement.
- Auxiliary Space:O(N).
The use of hash_map data structure for mapping characters consumes N space.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.