Check if the given string follows the given pattern
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The "word pattern" problem asks whether a string of space-separated words maps one-to-one with a pattern string of characters. For example, pattern "abba" and string "dog cat cat dog" match because a→dog and b→cat are consistent in both directions. This is a classic hash-map problem (LeetCode 290) that tests bijection — every pattern character maps to exactly one word, and every word maps to exactly one pattern character. The solution runs in O(n) time using two dictionaries.
Hash Map Approach (Python)
Two dictionaries enforce the bijection: char_to_word maps pattern characters to words, and word_to_char maps words back to pattern characters. If either direction conflicts, the pattern does not match.
Java Implementation
Index-Based Approach
An alternative uses the index of first occurrence for both the character and the word at each position:
This is concise but runs in O(n²) because index() scans the list each time. The hash-map approach is O(n).
Isomorphic Strings Variant
A closely related problem is checking if two strings are isomorphic (LeetCode 205), where each character in string s maps to exactly one character in string t:
The logic is identical — the only difference is that word pattern splits a string into words while isomorphic strings compares character by character.
Regex Pattern Matching Variant
For actual regex-style pattern matching where the pattern contains wildcards, use the re module:
This is a different problem — regex matching checks character-level patterns, not word-level bijections.
Common Pitfalls
- Only checking one direction of the mapping: Mapping
char→wordalone is not enough. Pattern"abba"with string"dog dog dog dog"would incorrectly return true becausea→dogandb→dogare both valid forward mappings. The reverse mapword→charcatches this by detecting thatdogmaps to bothaandb. - Forgetting to check length equality: If the pattern has 4 characters but the string has 3 words, the answer is always false. Skipping this check can cause index errors or incorrect matches on partial data.
- Using
split()vssplit(" ")incorrectly: In Python,"a b".split()produces["a", "b"](splits on any whitespace, removes empties), while"a b".split(" ")produces["a", "", "b"](splits on single space, keeps empties). Usesplit()for the word pattern problem to handle multiple spaces. - Confusing this problem with regex matching: The word pattern problem checks bijection between pattern characters and words. It has nothing to do with regex wildcards like
*,+, or.. These are fundamentally different problems. - Not handling empty inputs: An empty pattern with an empty string should return true. An empty pattern with a non-empty string (or vice versa) should return false. The length check handles this, but edge cases are easy to miss in interviews.
Summary
- Use two hash maps to enforce a one-to-one (bijective) mapping between pattern characters and words
- Check both directions:
char→wordandword→char— one direction alone allows false positives - Always verify that the pattern length equals the word count before comparing
- Time complexity is O(n) with hash maps, O(n²) with the index-based approach
- The same bijection technique solves isomorphic strings (LeetCode 205) and similar mapping problems

