4.5 KiB
Word Pattern
Problem Number: 290 Difficulty: Easy Category: Hash Table, String LeetCode Link: https://leetcode.com/problems/word-pattern/
Problem Description
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.
Example 1:
Input: pattern = "abba", s = "dog cat cat dog"
Output: true
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 <= 300patterncontains only lower-case English letters.1 <= s.length <= 3000scontains only lowercase English letters and spaces' '.sdoes not contain any leading or trailing spaces.- All the words in
sare separated by a single space.
My Approach
I used a Hash Table approach to check if the string follows the pattern. The key insight is to establish a bijection between pattern characters and words, ensuring one-to-one mapping.
Algorithm:
- Split string s into words
- Check if pattern length equals number of words
- First pass: establish mappings for new pattern characters
- Second pass: verify all mappings are consistent
- Return True if all checks pass
Solution
The solution uses hash table to establish and verify bijection between pattern and words. See the implementation in the solution file.
Key Points:
- Uses hash table for character-to-word mapping
- Two-pass approach for verification
- Checks bijection requirement
- Handles length mismatch case
Time & Space Complexity
Time Complexity: O(n)
- Split operation: O(n)
- Two passes through pattern/words: O(n)
- Total: O(n)
Space Complexity: O(k)
- Hash table stores character mappings
- k is number of unique characters in pattern
- In worst case: O(n)
Key Insights
-
Bijection: Each pattern character must map to exactly one word, and vice versa.
-
Two-Pass Approach: First pass establishes mappings, second pass verifies consistency.
-
Length Check: Pattern length must equal number of words.
-
Hash Table: Using hash table provides O(1) lookup for mappings.
-
One-to-One Mapping: No two characters can map to the same word.
-
Consistency Check: All mappings must be consistent throughout the string.
Mistakes Made
-
Single Pass: Initially might try single pass without proper verification.
-
Wrong Mapping: Not checking bijection requirement properly.
-
Complex Logic: Overcomplicating the mapping verification.
-
Length Mismatch: Not handling pattern length vs word count mismatch.
Related Problems
- Isomorphic Strings (Problem 205): Check string isomorphism
- Valid Anagram (Problem 242): Check if strings are anagrams
- Group Anagrams (Problem 49): Group strings by anagrams
- Longest Substring Without Repeating Characters (Problem 3): Find unique substring
Alternative Approaches
- Two Hash Tables: Use separate hash tables for pattern->word and word->pattern - O(n) time, O(n) space
- Single Pass: Use single pass with immediate verification - O(n) time, O(n) space
- Array Mapping: Use arrays for ASCII characters - O(n) time, O(1) space
Common Pitfalls
- Single Pass: Using single pass without proper verification.
- Wrong Mapping: Not checking bijection requirement properly.
- Complex Logic: Overcomplicating the mapping verification.
- Length Mismatch: Not handling pattern length vs word count mismatch.
- Space Inefficiency: Using unnecessary data structures.
Note: This is a hash table problem that demonstrates efficient bijection checking between pattern and words.