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.
// Function to check if a pattern matches a word
function isPatternMatch(pattern, word) {
// Check if the lengths of the pattern
// and word are different, if so, they cannot match
if (pattern.length !== word.length) {
return false;
}
// Create maps to store mappings from characters
// in pattern to characters in word and vice versa
const patternToWordMap = new Map();
const wordToPatternMap = new Map();
// Iterate through each character
// in the pattern and word
for (let i = 0; i < pattern.length; i++) {
const charInPattern = pattern[i];
const charInWord = word[i];
// Check if the character in the pattern
// is already mapped to a character in the word
if (patternToWordMap.has(charInPattern)
&& patternToWordMap.get(charInPattern)
!== charInWord) {
return false; // Pattern doesn't match the word
}
// Check if the character in the word is
// already mapped to a character in the pattern
if (wordToPatternMap.has(charInWord)
&& wordToPatternMap.get(charInWord)
!== charInPattern) {
return false; // Pattern doesn't match the word
}
// Create mappings for the
// character in the pattern and word
patternToWordMap
.set(charInPattern, charInWord);
wordToPatternMap
.set(charInWord, charInPattern);
}
return true; // Pattern matches the word
}
// Function to find and print words
// in the dictionary that match the given pattern
function findMatchingWords(dictionary, pattern) {
let result = "";
// Iterate through each word in the dictionary
for (let word of dictionary) {
// Check if the word matches the pattern
if (isPatternMatch(pattern, word)) {
// Add matching words to the result
result += word + " ";
}
}
if (result === "") {
console.log("No matches found.");
} else {
// Print the matching words
console.log(result);
}
}
// Driver code
let wordDictionary = new Set();
wordDictionary.add("aaa");
wordDictionary.add("bbb");
wordDictionary.add("ccc");
wordDictionary.add("xyr");
let inputPattern = "nnn";
findMatchingWords(wordDictionary, inputPattern)
Output
aaa bbb ccc
JavaScript Program to Find all Strings that Match Specific Pattern in a Dictionary
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.