JavaScript Program to Find all Strings that Match Specific Pattern in a Dictionary
Last Updated :
31 Oct, 2023
Finding all strings in a dictionary that match a specific pattern in JavaScript involves searching for words that follow a particular structure or format within a given dictionary. The pattern can include placeholders for characters, allowing flexibility in the search. In this article, we will explore various approaches for finding all strings that match specific patterns in a dictionary in JavaScript.
Examples:
Input: words= ["sss", "mmm", "tyu", "abc"];
pattern = "aaa"
Output: ["sss" "mmm"]
Explanation: sss and mmm have,similar pattern.
Input: words = ["123", "112", "456", "133"];
pattern = "mno"
Output: ["123" "456"]
Explanation: 123 and 456 have,similar pattern.
Method 1: Naive Approach
The objective is to determine if a word shares the same structural pattern as the given pattern. One way to tackle this challenge is by creating a unique numerical representation for both the word and the pattern and then checking if they match. To put it simply, we assign distinct numerical values to the individual characters within the word, creating a sequence of integers (referred to as the word’s hash) based on the frequency of each character. Subsequently, we compare this hash with the pattern’s hash to establish if they are identical.
Syntax:
let characterMap = new Map();
Example: Below is the implementation of the above approach.
Javascript
function encodeString(inputString) {
let characterMap = new Map();
let encodedString = "" ;
let uniqueCharacterIndex = 0;
for (let character of inputString) {
if (!characterMap.has(character)) {
characterMap.
set(character, uniqueCharacterIndex++);
}
encodedString += characterMap.get(character);
}
return encodedString;
}
function findMatchingWords(wordList, pattern) {
let patternLength = pattern.length;
let patternHash = encodeString(pattern);
for (let word of wordList) {
if (word.length === patternLength
&& encodeString(word) === patternHash) {
console.log(word);
}
}
}
let wordList =
[ "123" , "122" , "155" , "177" ];
let pattern = "rss" ;
findMatchingWords(wordList, pattern);
|
Method 2: Efficient approach
We directly associate the pattern’s letters with the corresponding letters in the word. When processing the current character, if it hasn’t been mapped yet, we map it to the corresponding character in the word. If it’s already mapped, we verify if the previous mapping matches the current character in the word.
Syntax:
let wordDictionary = new Set();
Example: Below is the implementation of the above approach.
Javascript
function isPatternMatch(pattern, word) {
if (pattern.length !== word.length) {
return false ;
}
const patternToWordMap = new Map();
const wordToPatternMap = new Map();
for (let i = 0; i < pattern.length; i++) {
const charInPattern = pattern[i];
const charInWord = word[i];
if (patternToWordMap.has(charInPattern)
&& patternToWordMap.get(charInPattern)
!== charInWord) {
return false ;
}
if (wordToPatternMap.has(charInWord)
&& wordToPatternMap.get(charInWord)
!== charInPattern) {
return false ;
}
patternToWordMap
.set(charInPattern, charInWord);
wordToPatternMap
.set(charInWord, charInPattern);
}
return true ;
}
function findMatchingWords(dictionary, pattern) {
let result = "" ;
for (let word of dictionary) {
if (isPatternMatch(pattern, word)) {
result += word + " " ;
}
}
if (result === "" ) {
console.log( "No matches found." );
} else {
console.log(result);
}
}
let wordDictionary = new Set();
wordDictionary.add( "aaa" );
wordDictionary.add( "bbb" );
wordDictionary.add( "ccc" );
wordDictionary.add( "xyr" );
let inputPattern = "nnn" ;
findMatchingWords(wordDictionary, inputPattern)
|
Share your thoughts in the comments
Please Login to comment...