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.
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
Method 1:
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
C++
#include <bits/stdc++.h>
using namespace std;
string encodeString(string str)
{
unordered_map< char , int > map;
string res = "" ;
int i = 0;
for ( char ch : str) {
if (map.find(ch) == map.end())
map[ch] = i++;
res += to_string(map[ch]);
}
return res;
}
void findMatchedWords(unordered_set<string> dict,
string pattern)
{
int len = pattern.length();
string hash = encodeString(pattern);
for (string word : dict) {
if (word.length() == len
&& encodeString(word) == hash)
cout << word << " " ;
}
}
int main()
{
unordered_set<string> dict = { "abb" , "abc" ,
"xyz" , "xyy" };
string pattern = "foo" ;
findMatchedWords(dict, pattern);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static String encodeString(String str)
{
HashMap<Character, Integer> map = new HashMap<>();
String res = "" ;
int i = 0 ;
char ch;
for ( int j = 0 ; j < str.length(); j++) {
ch = str.charAt(j);
if (!map.containsKey(ch))
map.put(ch, i++);
res += map.get(ch);
}
return res;
}
static void findMatchedWords(
String[] dict, String pattern)
{
int len = pattern.length();
String hash = encodeString(pattern);
for (String word : dict) {
if (word.length() == len
&& encodeString(word).equals(hash))
System.out.print(word + " " );
}
}
public static void main(String args[])
{
String[] dict = { "abb" , "abc" ,
"xyz" , "xyy" };
String pattern = "foo" ;
findMatchedWords(dict, pattern);
}
}
|
Python3
def encodeString( Str ):
map = {}
res = ""
i = 0
for ch in Str :
if ch not in map :
map [ch] = i
i + = 1
res + = str ( map [ch])
return res
def findMatchedWords( dict , pattern):
Len = len (pattern)
hash = encodeString(pattern)
for word in dict :
if ( len (word) = = Len and
encodeString(word) = = hash ):
print (word, end = " " )
dict = [ "abb" , "abc" , "xyz" , "xyy" ]
pattern = "foo"
findMatchedWords( dict , pattern)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static String encodeString(String str)
{
Dictionary< char , int > map = new Dictionary< char , int >();
String res = "" ;
int i = 0;
char ch;
for ( int j = 0; j < str.Length; j++) {
ch = str[j];
if (!map.ContainsKey(ch))
map.Add(ch, i++);
res += map[ch];
}
return res;
}
static void findMatchedWords(String[] dict, String pattern)
{
int len = pattern.Length;
String hash = encodeString(pattern);
foreach (String word in dict)
{
if (word.Length == len && encodeString(word).Equals(hash))
Console.Write(word + " " );
}
}
public static void Main(String[] args)
{
String[] dict = { "abb" , "abc" , "xyz" , "xyy" };
String pattern = "foo" ;
findMatchedWords(dict, pattern);
}
}
|
Javascript
<script>
function encodeString(str)
{
let map = new Map();
let res = "" ;
let i = 0;
let ch;
for (let j = 0; j < str.length; j++) {
ch = str[j];
if (!map.has(ch))
map.set(ch, i++);
res += map.get(ch);
}
return res;
}
function findMatchedWords(dict, pattern)
{
let len = pattern.length;
let hash = encodeString(pattern);
for (let word=0;word< dict.length;word++) {
if (dict[word].length == len
&& encodeString(dict[word]) == (hash))
document.write(dict[word] + " " );
}
}
let dict=[ "abb" , "abc" , "xyz" , "xyy" ];
let pattern = "foo" ;
findMatchedWords(dict, pattern);
</script>
|
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.
Method 2:
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 :
- 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)
C++
#include <bits/stdc++.h>
using namespace std;
bool check(string pattern, string word)
{
if (pattern.length() != word.length())
return false ;
char ch[128] = { 0 };
char map_word[128]={ 0};
int len = word.length();
for ( int i = 0; i < len; i++) {
if (ch[pattern[i]] == 0 && map_word[word[i] ]==0)
{
ch[pattern[i]] = word[i];
map_word[word[i] ]=pattern[i];
}
else if (ch[pattern[i]] != word[i] || map_word[word[i] ]!=pattern[i])
return false ;
}
return true ;
}
void findMatchedWords(unordered_set<string> dict,
string pattern)
{
int len = pattern.length();
for (string word : dict) {
if (check(pattern, word))
cout << word << " " ;
}
}
int main()
{
unordered_set<string> dict = { "abb" , "abc" , "xyz" , "xyy" , "bbb" };
string pattern = "foo" ;
findMatchedWords(dict, pattern);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean check(String pattern, String word)
{
if (pattern.length() != word.length())
return false ;
int [] ch = new int [ 128 ];
int Len = word.length();
for ( int i = 0 ; i < Len; i++)
{
if (ch[( int )pattern.charAt(i)] == 0 )
{
ch[( int )pattern.charAt(i)] = word.charAt(i);
}
else if (ch[( int )pattern.charAt(i)] != word.charAt(i))
{
return false ;
}
}
return true ;
}
static void findMatchedWords(HashSet<String> dict, String pattern)
{
int Len = pattern.length();
String result = " " ;
for (String word : dict)
{
if (check(pattern, word))
{
result = word + " " + result;
}
}
System.out.print(result);
}
public static void main(String[] args) {
HashSet<String> dict = new HashSet<String>();
dict.add( "abb" );
dict.add( "abc" );
dict.add( "xyz" );
dict.add( "xyy" );
String pattern = "foo" ;
findMatchedWords(dict, pattern);
}
}
|
Python3
def check(pattern, word):
if ( len (pattern) ! = len (word)):
return False
ch = [ 0 for i in range ( 128 )]
Len = len (word)
for i in range ( Len ):
if (ch[ ord (pattern[i])] = = 0 ):
ch[ ord (pattern[i])] = word[i]
else if (ch[ ord (pattern[i])] ! = word[i]):
return False
return True
def findMatchedWords( Dict , pattern):
Len = len (pattern)
for word in range ( len ( Dict ) - 1 , - 1 , - 1 ):
if (check(pattern, Dict [word])):
print ( Dict [word], end = " " )
Dict = [ "abb" , "abc" , "xyz" , "xyy" ]
pattern = "foo"
findMatchedWords( Dict , pattern)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static bool check( string pattern, string word)
{
if (pattern.Length != word.Length)
return false ;
int [] ch = new int [128];
int Len = word.Length;
for ( int i = 0; i < Len; i++)
{
if (ch[( int )pattern[i]] == 0)
{
ch[( int )pattern[i]] = word[i];
}
else if (ch[( int )pattern[i]] != word[i])
{
return false ;
}
}
return true ;
}
static void findMatchedWords(HashSet< string > dict,
string pattern)
{
int Len = pattern.Length;
string result = " " ;
foreach ( string word in dict)
{
if (check(pattern, word))
{
result = word + " " + result;
}
}
Console.Write(result);
}
static void Main()
{
HashSet< string > dict = new HashSet< string >(
new string []{ "abb" , "abc" , "xyz" , "xyy" });
string pattern = "foo" ;
findMatchedWords(dict, pattern);
}
}
|
Javascript
<script>
function check(pattern, word)
{
if (pattern.length != word.length)
return false ;
let ch = new Array(128);
for (let i=0;i<128;i++)
{
ch[i]=0;
}
let Len = word.length;
for (let i = 0; i < Len; i++)
{
if (ch[pattern[i].charCodeAt(0)] == 0)
{
ch[pattern[i].charCodeAt(0)] = word[i];
}
else if (ch[pattern[i].charCodeAt(0)] != word[i])
{
return false ;
}
}
return true ;
}
function findMatchedWords(dict,pattern)
{
let Len = pattern.length;
let result = " " ;
for (let word of dict.values())
{
if (check(pattern, word))
{
result = word + " " + result;
}
}
document.write(result);
}
let dict = new Set();
dict.add( "abb" );
dict.add( "abc" );
dict.add( "xyz" );
dict.add( "xyy" );
let pattern = "foo" ;
findMatchedWords(dict, pattern);
</script>
|
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.
Last Updated :
04 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...