Find and Replace Pattern - Problem

Imagine you're a code-breaking detective! 🕵️‍♀️ You have a secret pattern and a list of words, and you need to find which words could be the result of applying a secret cipher to your pattern.

Here's the twist: the cipher is a bijection - each letter in the pattern maps to exactly one letter in the word, and no two pattern letters can map to the same word letter. It's like a perfect one-to-one substitution cipher!

For example:
• Pattern: "abb"
• Word: "mee"
• Mapping: a→m, b→e ✅ Valid!
• Word: "mff"
• Mapping: a→m, b→f ✅ Valid!
• Word: "mef"
• Would need: a→m, b→e, b→f ❌ Invalid! (b can't map to both e and f)

Your mission: Return all words from the input list that match the given pattern according to these cipher rules.

Input & Output

example_1.py — Basic Pattern Matching
$ Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
💡 Note: "mee" matches pattern "abb" with mapping a→m, b→e. "aqq" matches with mapping a→a, b→q. Other words fail because they would require invalid mappings (like "abc" needing a→a, b→b, b→c which violates bijection).
example_2.py — Single Character Pattern
$ Input: words = ["a","aa","aaa"], pattern = "a"
Output: ["a"]
💡 Note: Only "a" matches the single character pattern "a". The other words have different lengths and cannot match.
example_3.py — Complex Pattern
$ Input: words = ["badc","abcd","efgh","abab"], pattern = "abba"
Output: ["abab"]
💡 Note: Only "abab" matches pattern "abba" with mapping a→a, b→b. "badc" would need a→b, b→a, b→d, a→c which creates conflicts.

Visualization

Tap to expand
🕵️‍♀️ Secret Code Decoder VisualizationIntercepted Pattern: "abb" | Testing Message: "mee"Agent Mission: Validate if message follows the pattern's cipherCipher Book APattern → Messagea → m ✓b → e ✓Cipher Book BMessage → Patternm → a ✓e → b ✓🔍 Decoding ProcessStep 1: Position 0✓ 'a' ↔ 'm' - New mapping recordedStep 2: Position 1✓ 'b' ↔ 'e' - New mapping recordedStep 3: Position 2✓ 'b' ↔ 'e' - Consistent with existing!🎯 MISSION SUCCESS!Message "mee" successfully decoded using pattern "abb"Both cipher books remain consistent throughout the process
Understanding the Visualization
1
Receive Coded Message
A new intercepted message arrives for pattern matching
2
Initialize Decoder Maps
Set up two cipher books: pattern→message and message→pattern
3
Character-by-Character Decode
Process each position, updating both cipher books
4
Conflict Detection
If any mapping conflicts with existing entries, reject the message
5
Success or Failure
Message is valid if all characters decode consistently
Key Takeaway
🎯 Key Insight: The bijection property requires validation in BOTH directions - pattern→word AND word→pattern mappings must be consistent to ensure a valid one-to-one cipher relationship.

Time & Space Complexity

Time Complexity
⏱️
O(n * m)

Where n is the number of words and m is the length of each word. We process each character exactly once.

n
2n
Linear Growth
Space Complexity
O(m)

We need space for two hash maps, each storing at most m key-value pairs (the length of the pattern).

n
2n
Linear Space

Constraints

  • 1 ≤ words.length ≤ 50
  • 1 ≤ words[i].length ≤ 20
  • words[i] and pattern consist of lowercase English letters
  • words[i].length == pattern.length
Asked in
Google 25 Facebook 18 Amazon 15 Microsoft 12
89.5K Views
Medium Frequency
~12 min Avg. Time
3.4K 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