Try the Problem
You can practice the problem here:
https://leetcode.com/problems/word-pattern/
Problem Description
You are given:
- A string
pattern - A string
s
The string s consists of words separated by spaces.
Your task is to determine whether s follows the same pattern defined by the string pattern.
To follow the pattern, there must be a bijection between pattern characters and words.
Understanding the Meaning of Bijection
A bijection means a one-to-one relationship between two sets.
For this problem it means:
- Every character in the pattern maps to exactly one word.
- Every word maps to exactly one pattern character.
- Two characters cannot map to the same word.
- Two words cannot map to the same character.
This ensures the mapping is unique in both directions.
Example Walkthrough
Example 1
Input
Output
Explanation
Mapping:
Pattern:
Words:
The mapping is perfectly consistent.
Example 2
Input
Output
Explanation
The last word "fish" breaks the pattern because "a" was already mapped to "dog".
Example 3
Input
Output
Explanation
Pattern requires:
But the words differ.
Constraints
Core Observation
Before applying any algorithm, we should verify one simple condition.
If the number of pattern characters does not match the number of words, then the pattern cannot match.
Example:
Pattern length = 4
Words = 3
This automatically returns false.
Thinking About Possible Approaches
While solving this problem, several strategies may come to mind:
- Use one HashMap to store pattern → word mapping and check duplicates manually.
- Use two HashMaps to enforce bijection from both directions.
- Track first occurrence indices of pattern characters and words.
- Compare mapping patterns using arrays or maps.
Let’s explore these approaches one by one.
Approach 1: Single HashMap (Your Solution)
Idea
We store mapping:
While iterating:
- If the character already exists in the map
- → check if it maps to the same word.
- If it does not exist
- → ensure that no other character already maps to that word.
This ensures bijection.
Java Implementation
Time Complexity
Because containsValue() can take O(n) time in worst case.
Space Complexity
Approach 2: Two HashMaps (Cleaner Bijection Enforcement)
Idea
Instead of checking containsValue, we maintain two maps.
This ensures bijection naturally.
Java Implementation
Time Complexity
Because both maps allow constant-time lookups.
Space Complexity
Approach 3: Index Mapping Technique (Elegant Trick)
Idea
Another clever technique is to compare first occurrence indices.
Example:
Pattern index sequence:
Words index sequence:
If the index sequences match, then the pattern holds.
Java Implementation
Time Complexity
Space Complexity
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Notes |
| Single HashMap | O(n²) | O(n) | Simple but slower |
| Two HashMaps | O(n) | O(n) | Cleaner bijection enforcement |
| Index Mapping | O(n) | O(n) | Elegant and interview-friendly |
Key Interview Insight
Problems like this test your understanding of:
Similar problems include:
- Isomorphic Strings
- Word Pattern II
- Substitution Cipher Problems
Understanding how to enforce one-to-one mappings is a powerful technique.
Conclusion
The Word Pattern problem is an excellent example of how HashMaps can enforce relationships between two datasets.
While a simple HashMap solution works, more optimized methods like two-way mapping or index comparison provide cleaner and more efficient implementations.
Mastering these techniques will help you solve many pattern-matching and mapping problems commonly asked in coding interviews.




