![]() |
VOOZH | about |
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.
Examples:
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.
Example:
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
Algorithm :
Pseudo Code:
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
xyy abb
Complexity Analysis:
:
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.
Example:
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 patternAlgorithm :
- 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.
Pseudo Code:
for words in dictionary: char arr_map[128]=0 char map_word[128]=0 if(words.length==pattern.length) for 0 to length of pattern: if(arr_map[character in pattern]==0 && map_word[character in word]==0) arr_map[character in pattern]=word[character in word] map_word[character in word]=pattern[character in pattern] else if(arr_map[character]!=word[character] ||map_word[character]!=pattern[character] ) break the loop If above loop runs successfully Print(words)
xyy abb
Complexity Analysis: