Word Pattern - Problem

Imagine you're a cryptographer trying to decode a secret message! You have a pattern (like a cipher key) and a string of words, and you need to determine if the words follow the exact same pattern as your key.

Given a pattern string and a string s of words separated by spaces, determine if s follows the same pattern. This means there must be a one-to-one correspondence (bijection) between each letter in the pattern and each word in the string.

Rules:

  • Each letter in pattern maps to exactly one unique word in s
  • Each unique word in s maps to exactly one letter in pattern
  • No two letters can map to the same word
  • No two words can map to the same letter

Example: Pattern "abba" with string "dog cat cat dog" should return true because 'a' maps to 'dog', 'b' maps to 'cat', and the pattern matches perfectly!

Input & Output

example_1.py β€” Basic Pattern Match
$ Input: pattern = "abba", s = "dog cat cat dog"
β€Ί Output: true
πŸ’‘ Note: The pattern matches perfectly: 'a' maps to 'dog', 'b' maps to 'cat', and the sequence follows the same pattern in both directions.
example_2.py β€” Pattern Mismatch
$ Input: pattern = "abba", s = "dog cat cat fish"
β€Ί Output: false
πŸ’‘ Note: The pattern fails because 'a' would need to map to both 'dog' and 'fish', violating the one-to-one correspondence rule.
example_3.py β€” Length Mismatch
$ Input: pattern = "aaaa", s = "dog dog dog"
β€Ί Output: false
πŸ’‘ Note: The pattern has 4 characters but there are only 3 words, so it's impossible for them to match.

Visualization

Tap to expand
πŸ•΅οΈ Secret Code Validation ProcessInput Pattern: a b b aCode Words: dog cat cat dogπŸ“š Cipher Book (char β†’ word)Step 1: a β†’ dogStep 2: b β†’ catValidation:βœ“ Step 3: b β†’ cat (confirmed)βœ“ Step 4: a β†’ dog (confirmed)πŸ” Reverse Lookup (word β†’ char)Step 1: dog β†’ aStep 2: cat β†’ bValidation:βœ“ Step 3: cat β†’ b (confirmed)βœ“ Step 4: dog β†’ a (confirmed)🎯 Validation Result: SUCCESS!βœ“ Each character maps to exactly one unique wordβœ“ Each word maps to exactly one unique characterBidirectionalCheck
Understanding the Visualization
1
Initialize Cipher Books
Create two lookup tables: char→word and word→char
2
Process First Pair (a, dog)
No existing mapping, so establish: a→dog and dog→a
3
Process Second Pair (b, cat)
No existing mapping, so establish: b→cat and cat→b
4
Validate Third Pair (b, cat)
Check: b should map to cat βœ“, cat should map to b βœ“
5
Validate Fourth Pair (a, dog)
Check: a should map to dog βœ“, dog should map to a βœ“
6
Cipher Validated
All pairs consistent - the secret message follows the pattern!
Key Takeaway
🎯 Key Insight: The bidirectional hash table approach ensures perfect cipher validation by maintaining both charβ†’word AND wordβ†’char mappings, catching any violation of the one-to-one correspondence immediately in O(1) time per element.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through pattern and words, with O(1) hash map operations

n
2n
βœ“ Linear Growth
Space Complexity
O(n)

Two hash maps storing at most n character-word pairs each

n
2n
⚑ Linearithmic Space

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
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22 Apple 15
52.0K Views
High Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen