Given a dictionary of words, find all strings that matches the given pattern where every character in the pattern is uniquely mapped to a character in the dictionary.
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 above approach and store the corresponding hash of pattern in a string variable hash.
- Algorithm to encode -:
- Initialise 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 same algorithm .
- If the hash of current word is equal to 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 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 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 pattern with corresponding character of word.
- Firstly check whether the length of word and pattern is equal or not, if no then check 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, the check whether the character with which it has been mapped is equal to the current character of the word.
- If no then word does not follow the given pattern.
- If word follows the pattern till 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 contribute.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Case-specific Sorting of Strings
- Case-specific sorting of Strings in O(n) time and O(1) space
- Program to find all match of a regex in a string
- Print Strings In Reverse Dictionary Order Using Trie
- Count strings that end with the given pattern
- Generate all binary strings from given pattern
- Python Dictionary to find mirror characters in a string
- Dictionary and counter in Python to find winner of election
- Given a sorted dictionary of an alien language, find order of characters
- Find largest word in dictionary by deleting some characters of given string
- Find strings that end with a given suffix
- Find all palindromic sub-strings of a given string | Set 2
- Find if a string is interleaved of two other strings | DP-33
- Find uncommon characters of the two strings | Set 2
- Given two strings, find if first string is a subsequence of second
- Find uncommon characters of the two strings
- Given two numbers as strings, find if one is a power of other
- Find all distinct palindromic sub-strings of a given string
- Find a string in lexicographic order which is in between given two strings
- Find the Mid-Alphabet for each index of the given Pair of Strings