0% completed
Problem Statement
Given a pattern
and a string s
, return true
if the string s
follows the same pattern
.
Here, the following the pattern means each character
in the pattern should map
to a single word
in s
, and each word in s
should map to a single character
in the pattern.
Examples
Example 1:
- Input: pattern =
"eegg"
, s ="dog dog cat cat"
- Output:
true
- Explanation: The pattern "eegg" corresponds to the words "dog dog cat cat". Both 'e's map to "dog" and both 'g's map to "cat".
Example 2:
- Input: pattern =
"abca"
, s ="one two three four"
- Output:
false
- Explanation: Here,
a
maps to the"one"
and"four"
both. So, the string doesn't follow the same pattern.
Example 3:
- Input: pattern =
"abacac"
, s ="dog cat dog mouse dog mouse"
- Output:
true
- Explanation: The pattern "abacac" corresponds to the words "dog cat dog mouse dog mouse". 'a' maps to "dog", 'b' maps to "cat", and 'c' maps to "mouse".
Constraints:
- 1 <= pattern.length <= 300
- pattern contains only lower-case English letters.
- 1 <= s.length <= 3000
s
contains only lowercase English letters and spaces ' '.s
does not contain any leading or trailing spaces.- All the words in s are separated by a single space.
Solution
To solve this problem, we need to establish a one-to-one correspondence between each character in the pattern and each word in the string s. This can be effectively managed using two hash maps: one to map characters from the pattern to words in s, and another to map words in s to characters in the pattern. This dual mapping ensures that the relationship is consistent in both directions. If at any point we find that a character or word does not map as expected, we can conclude that string does not follow the pattern.
This approach is efficient because it allows us to quickly check and establish the required mappings. The use of hash maps ensures that our checks and insertions are done in constant time on average, making the overall approach fast and reliable.
Step-by-step Algorithm
- Split the string s into an array of words.
- Check if the length of the pattern matches the number of words in s. If not, return false.
- Initialize two hash maps: one for pattern to word mapping (
char_to_word
), and one for word to pattern mapping (word_to_char
). - Iterate over each character in the pattern and the corresponding word in s:
- Check if the character is already mapped:
- If it is, ensure it maps to the current word.
- If it does not match, return false.
- Else, add character and word to 'char_to_word' map.
- Check if the word is already mapped:
- If it is, ensure it maps to the current character.
- If it does not match, return false.
- Else, add word and character to 'word_to_char' map.
- Check if the character is already mapped:
- Return true if all characters and words map correctly.
Algorithm Walkthrough
Input: pattern = "abacac"
, s = "dog cat dog mouse dog mouse"
-
Initial Input:
- Pattern:
abacac
- String s:
dog cat dog mouse dog mouse
- Pattern:
-
Step 1: Split the string s into words:
- Words:
["dog", "cat", "dog", "mouse", "dog", "mouse"]
- Words:
-
Step 2: Check length:
- Length of pattern: 6
- Number of words: 6
- Lengths match, proceed to the next step.
-
Step 3: Initialize hash maps:
charToWord = {}
wordToChar = {}
-
Step 4: Iterate over each character and word:
-
Iteration 1:
- Character:
a
- Word:
dog
a
is not incharToWord
, anddog
is not inwordToChar
.- Map
a
todog
:charToWord = {'a': 'dog'}
- Map
dog
toa
:wordToChar = {'dog': 'a'}
- Character:
-
Iteration 2:
- Character:
b
- Word:
cat
b
is not incharToWord
, andcat
is not inwordToChar
.- Map
b
tocat
:charToWord = {'a': 'dog', 'b': 'cat'}
- Map
cat
tob
:wordToChar = {'dog': 'a', 'cat': 'b'}
- Character:
-
Iteration 3:
- Character:
a
- Word:
dog
a
is incharToWord
, and it maps todog
.dog
is inwordToChar
, and it maps toa
.- Mappings are consistent, proceed to the next iteration.
- Character:
-
Iteration 4:
- Character:
c
- Word:
mouse
c
is not incharToWord
, andmouse
is not inwordToChar
.- Map
c
tomouse
:charToWord = {'a': 'dog', 'b': 'cat', 'c': 'mouse'}
- Map
mouse
toc
:wordToChar = {'dog': 'a', 'cat': 'b', 'mouse': 'c'}
- Character:
-
Iteration 5:
- Character:
a
- Word:
dog
a
is incharToWord
, and it maps todog
.dog
is inwordToChar
, and it maps toa
.- Mappings are consistent, proceed to the next iteration.
- Character:
-
Iteration 6:
- Character:
c
- Word:
mouse
c
is incharToWord
, and it maps tomouse
.mouse
is inwordToChar
, and it maps toc
.- Mappings are consistent.
- Character:
-
-
Step 5: All characters and words are mapped correctly.
- Return
true
.
- Return
Code
Complexity Analysis
- Time Complexity: O(n), where n is the length of the pattern or the number of words in s. We iterate through the pattern and the words once, performing constant-time operations (hash map lookups and insertions) at each step.
- Space Complexity: O(n), where n is the number of unique characters in the pattern or unique words in s. In the worst case, we store every character and word in the hash maps.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible