"Word Pattern II" is the "Medium" version of the previous problem. Here, the string s is not space-separated. You must determine if there exists a way to split s into non-empty substrings such that they follow the pattern.
This is a classic Backtracking problem. Companies like Oracle and Uber ask this to see if you can explore a search space where the boundaries of the "words" are unknown. It requires trying all possible substring lengths for each character in the pattern and backtracking if a choice leads to a dead end.
The pattern is Backtracking with a Hash Map.
Pattern: aba, String: redblue? No, redbluered.
a can be r. Then b must be edblue. Then a must be r. redbluer != redbluered.a can be re. Then b must be dblu. Then a must be re. No.a can be red. Then b must be blue. Then a must be red.
red + blue + red = redbluered. Match found!Backtracking is about "Make a choice, recurse, undo the choice." For this problem, the choice is the length of the substring mapped to the current pattern character.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Split a String Into the Max Number of Unique Substrings | Medium | Solve | |
| Letter Combinations of a Phone Number | Medium | Solve | |
| Palindrome Permutation II | Medium | Solve | |
| Letter Tile Possibilities | Medium | Solve | |
| Find Unique Binary String | Medium | Solve |