Given a pattern
and a string s
, find if s
follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern
and a non-empty word in s
. Specifically:
pattern
maps to exactly one unique word in s
.s
maps to exactly one letter in pattern
.Example 1:
Input: pattern = "abba", s = "dog cat cat dog"
Output: true
Explanation:
The bijection can be established as:
'a'
maps to "dog"
.'b'
maps to "cat"
.Example 2:
Input: pattern = "abba", s = "dog cat cat fish"
Output: false
Example 3:
Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false
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.s
are separated by a single space.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to the Word Pattern problem involves trying every possible association between letters in the pattern and words in the string. We aim to find one valid mapping where each letter corresponds to exactly one word and each word corresponds to exactly one letter. We exhaustively check each possible pairing until we either find a match or run out of possibilities.
Here's how the algorithm would work step-by-step:
def word_pattern(pattern, string):
words = string.split()
if len(pattern) != len(words):
return False
letter_to_word = {}
word_to_letter = {}
# Iterate through each letter/word to check for conflicts
for i, letter in enumerate(pattern):
word = words[i]
# Check if the letter already maps to a different word
if letter in letter_to_word:
if letter_to_word[letter] != word:
return False
# Check if the word already maps to a different letter
elif word in word_to_letter:
if word_to_letter[word] != letter:
return False
# If no conflict, create the new mapping
else:
letter_to_word[letter] = word
word_to_letter[word] = letter
return True
The goal is to check if a pattern (like 'abba') matches the structure of a string of words (like 'dog cat cat dog'). We want to ensure each letter in the pattern consistently corresponds to a specific word, and vice versa. This is done by keeping track of the relationships between letters and words, ensuring there are no contradictions.
Here's how the algorithm would work step-by-step:
def word_pattern(pattern, input_string):
words = input_string.split()
if len(pattern) != len(words):
return False
letter_to_word = {}
word_to_letter = {}
for i in range(len(pattern)):
letter = pattern[i]
word = words[i]
# Ensures pattern and string are consistent
if letter in letter_to_word:
if letter_to_word[letter] != word:
return False
else:
letter_to_word[letter] = word
# Ensures string and pattern are consistent
if word in word_to_letter:
if word_to_letter[word] != letter:
return False
else:
word_to_letter[word] = letter
return True
Case | How to Handle |
---|---|
Empty pattern or empty string s | Return true if both pattern and s are empty, and false if only one is empty, as that means they cannot match the defined pattern. |
Pattern is longer than the number of words in s | Return false because there aren't enough words in s to correspond to each character in the pattern. |
Number of words in s is greater than the length of the pattern | Return false because there are too many words in s, and they can't possibly match the pattern exactly. |
One pattern character maps to multiple different words in s | The hash map should prevent this; if we try to map the same pattern character to a different word, return false. |
One word in s maps to multiple different characters in pattern | Use a second hash map for reverse mapping to ensure that each word maps back to a unique character, otherwise, return false. |
Pattern contains duplicate characters, but s contains unique words. | The solution must allow the same character in the pattern to map to the same word in s, while maintaining the one-to-one correspondence. |
s contains duplicate words, but pattern contains unique characters. | The solution must allow different characters in the pattern to map to different words in s while ensuring words map back to unique characters. |
Very long pattern and string (scaling concerns) | The hash map approach has a time complexity of O(N) where N is the maximum length of the pattern or s, making it scalable even with large inputs. |