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
pattern
string containing only lowercase letters. - A string
s
consisting of words separated by spaces.
Return true
if s
follows the same pattern as pattern
, i.e.:
- Each character in
pattern
maps to exactly one word ins
. - Each word in
s
maps 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
pattern
to a word ins
. - To ensure each word in
s
maps 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
s
into an array of words usingsplit(" ")
.
- Split the string
-
Check Lengths:
- If the length of
pattern
does not match the number of words ins
, returnfalse
.
- If the length of
-
Use Two Maps:
- Map each character in
pattern
to a word ins
. - Map each word in
s
back 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)
, wheren
is the length of pattern (or the number of words ins
).- Each character and word is processed once.
-
Space Complexity:
O(1)
, wherek
is the number of unique characters and words.- Two hash maps store up to
k
mappings.
- 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 -> word
andword -> char
ensures consistency.
- Highlight how mapping both
-
Discuss Edge Cases:
-
pattern
ands
lengthsdiffer: 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 (1)
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!