Word Pattern II - Problem
Word Pattern II is a fascinating pattern matching problem that challenges you to determine if a string can be split and mapped to match a given pattern.

You are given:

  • pattern - A string of lowercase letters (e.g., "abab")
  • s - A target string to match (e.g., "redblueredblue")

Goal: Return true if s matches the pattern through a bijective mapping.

What is a bijective mapping?

  • Each pattern character maps to exactly one non-empty substring
  • No two pattern characters can map to the same substring
  • Each pattern character must consistently map to the same substring

Example: Pattern "abab" and string "redblueredblue"

  • 'a' → "red" and 'b' → "blue"
  • This creates: "red" + "blue" + "red" + "blue" = "redblueredblue" ✓

Input & Output

example_1.py — Python
$ Input: pattern = "abab", s = "redblueredblue"
Output: true
💡 Note: We can map 'a' to 'red' and 'b' to 'blue'. This gives us: 'red' + 'blue' + 'red' + 'blue' = 'redblueredblue', which matches the input string perfectly.
example_2.py — Python
$ Input: pattern = "aaaa", s = "asdasdasdasd"
Output: true
💡 Note: We can map 'a' to 'asd'. This gives us: 'asd' + 'asd' + 'asd' + 'asd' = 'asdasdasdasd', which matches the input string.
example_3.py — Python
$ Input: pattern = "abab", s = "asdasdasdasd"
Output: false
💡 Note: There's no way to create a bijective mapping. If we try 'a' → 'asd' and 'b' → 'asd', this violates the bijective constraint (two different characters cannot map to the same substring).

Visualization

Tap to expand
🔐 Pattern Matching DecoderCipher Patterna b a bEncrypted Messager e d b l u e r e d b l u eDecode: a → redDecode: b → blueDecoded Resultred + blue + red + blue = redblueredblue ✓Successful Decode!Bijective mapping found
Understanding the Visualization
1
Start Decoding
Begin with pattern 'abab' and encrypted text 'redblueredblue'
2
First Mapping
Try mapping first 'a' to different substrings: 'r', 're', 'red'...
3
Consistent Check
When 'a' appears again, verify it maps to the same substring
4
Bijective Validation
Ensure no two different symbols map to the same word
5
Complete Match
Success when all symbols are consistently mapped and string is fully consumed
Key Takeaway
🎯 Key Insight: Use backtracking with bidirectional hash maps to maintain bijective constraints while exploring all possible substring mappings efficiently

Time & Space Complexity

Time Complexity
⏱️
O(s^p) worst case, much better in practice

Worst case is still exponential, but pruning and optimizations make it much faster for practical inputs

n
2n
Linear Growth
Space Complexity
O(p + m)

Space for recursion stack (p) plus hash maps storing at most m unique mappings

n
2n
Linear Space

Constraints

  • 1 ≤ pattern.length ≤ 20
  • 1 ≤ s.length ≤ 50
  • pattern contains only lowercase English letters
  • s contains only lowercase English letters
  • Each pattern character must map to a non-empty substring
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
28.8K Views
Medium Frequency
~25 min Avg. Time
1.2K 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