VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-all-strings-that-match-specific-pattern-in-a-dictionary/

⇱ Find all strings that match specific pattern in a dictionary - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find all strings that match specific pattern in a dictionary

Last Updated : 4 Jul, 2022

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
Recommended Practice

:

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 :

  • 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.

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

Output
xyy abb 

Complexity Analysis:

  • 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.

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 pattern

Algorithm :

  1. Create a character array in which we can map the characters of patterns with a corresponding character of a word.
  2. Firstly check whether the length of word and pattern is equal or not, if no then check the next word.
  3. 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.
  4. 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.
  5. If no then the word does not follow the given pattern.
  6. 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)

Output
xyy abb 

Complexity Analysis:

  • 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.
Comment
Article Tags:
Article Tags: