Top Interview 150
The Word Pattern problem tests your ability to handle bijective mappings between characters and words. Letβs solve LeetCode 290: Word Pattern step by step.
π Problem Description
Given:
- A
patternstring containing only lowercase letters. - A string
sconsisting of words separated by spaces.
Return true if s follows the same pattern as pattern, i.e.:
- Each character in
patternmaps to exactly one word ins. - Each word in
smaps to exactly one character inpattern.
π‘ Examples
Example 1
Input: pattern = "abba", s = "dog cat cat dog"
Output: true
Explanation: 'a' -> "dog", 'b' -> "cat".
Example 2
Input: pattern = "abba", s = "dog cat cat fish"
Output: false
Explanation: 'a' -> "dog", but 'b' cannot map to both "cat" and "fish".
Example 3
Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false
Explanation: 'a' -> "dog", but then cannot map to "cat".
π JavaScript Solution
We solve this problem using two hash maps:
- To map each character in
patternto a word ins. - To ensure each word in
smaps back to a unique character inpattern.
Implementation
var wordPattern = function(pattern, s) {
const words = s.split(" ");
if (pattern.length !== words.length) return false;
const charToWord = new Map();
const wordToChar = new Map();
for (let i = 0; i < pattern.length; i++) {
const char = pattern[i];
const word = words[i];
if ((charToWord.has(char) && charToWord.get(char) !== word) ||
(wordToChar.has(word) && wordToChar.get(word) !== char)) {
return false;
}
charToWord.set(char, word);
wordToChar.set(word, char);
}
return true;
};
π How It Works
-
Split Words:
- Split the string
sinto an array of words usingsplit(" ").
- Split the string
-
Check Lengths:
- If the length of
patterndoes not match the number of words ins, returnfalse.
- If the length of
-
Use Two Maps:
- Map each character in
patternto a word ins. - Map each word in
sback to a character inpattern.
- Map each character in
-
Validate Mappings:
- If a character or word conflicts with an existing mapping, return
false.
- If a character or word conflicts with an existing mapping, return
-
Return True:
- If all characters and words map consistently, return
true.
- If all characters and words map consistently, return
π Complexity Analysis
-
Time Complexity:
O(n), wherenis the length of pattern (or the number of words ins).- Each character and word is processed once.
-
Space Complexity:
O(1), wherekis the number of unique characters and words.- Two hash maps store up to
kmappings.
- Two hash maps store up to
π Dry Run
Input: pattern = "abba", s = "dog cat cat dog"

Output: true
β¨ Pro Tips for Interviews
-
Explain Two Maps:
- Highlight how mapping both
char -> wordandword -> charensures consistency.
- Highlight how mapping both
-
Discuss Edge Cases:
-
patternandslengthsdiffer: pattern = "abba",s = "dog cat". - Duplicate mappings:
pattern = "ab",s = "dog dog".
-
-
Follow-Up:
- How would you extend this solution to handle multi-character patterns or words?
π Learn More
Check out the full explanation and code walkthrough on my previous Dev.to post:
π Isomorphic Strings - JavaScript Solution
Whatβs your preferred method to solve this problem? Letβs discuss! π

Top comments (2)
Follow Me on GitHub π
If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.
Don't forget to follow for more updates!