Word Break II - Problem
Word Break II: Sentence Reconstruction Challenge
Imagine you have a string of characters with no spaces and a dictionary of valid words. Your mission is to add spaces strategically to transform this string into meaningful sentences where every word exists in your dictionary.
๐ฏ Goal: Find all possible ways to break the string into valid dictionary words
๐ Input: A string
๐ Output: All possible sentences formed by adding spaces
Example: Given
โข
โข
Note: Dictionary words can be reused multiple times in different combinations!
Imagine you have a string of characters with no spaces and a dictionary of valid words. Your mission is to add spaces strategically to transform this string into meaningful sentences where every word exists in your dictionary.
๐ฏ Goal: Find all possible ways to break the string into valid dictionary words
๐ Input: A string
s and an array wordDict of dictionary words๐ Output: All possible sentences formed by adding spaces
Example: Given
s = "catsanddog" and wordDict = ["cat", "cats", "and", "sand", "dog"], you could form:โข
"cats and dog"โข
"cat sand dog"Note: Dictionary words can be reused multiple times in different combinations!
Input & Output
example_1.py โ Basic Case
$
Input:
s = "catsanddog", wordDict = ["cat", "cats", "and", "sand", "dog"]
โบ
Output:
["cats and dog", "cat sand dog"]
๐ก Note:
Both "cats and dog" and "cat sand dog" are valid sentences. "cats" + "and" + "dog" and "cat" + "sand" + "dog" are both valid word combinations.
example_2.py โ Multiple Solutions
$
Input:
s = "pineapplepenapple", wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
โบ
Output:
["pine apple pen apple", "pineapple pen apple", "pine applepen apple"]
๐ก Note:
Three different ways to break the string: using "pine apple", "pineapple", or "pine applepen" at the beginning.
example_3.py โ No Solution
$
Input:
s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
โบ
Output:
[]
๐ก Note:
No valid way to break "catsandog" using the given dictionary words. "sandog" is not breakable into valid words.
Visualization
Tap to expand
Understanding the Visualization
1
Setup
Convert dictionary to hash set for O(1) lookup. Initialize memoization cache.
2
Try First Word
At each position, try all possible words that start from current position.
3
Recursively Solve
For each valid word found, recursively solve for the remaining substring.
4
Cache Results
Store results for each starting position to avoid redundant calculations.
5
Combine Solutions
Combine current word with all solutions from remaining string to build final sentences.
Key Takeaway
๐ฏ Key Insight: Memoization transforms an exponential problem into a manageable one by caching substring solutions, making Word Break II efficient even with multiple valid combinations!
Time & Space Complexity
Time Complexity
O(nยฒ + 2^n)
O(nยฒ) to check all substrings, plus O(2^n) for generating all possible combinations in worst case
โ Quadratic Growth
Space Complexity
O(nยฒ + 2^n)
O(nยฒ) for memoization table plus O(2^n) for storing all possible sentences
โ Quadratic Space
Constraints
- 1 โค s.length โค 20
- 1 โค wordDict.length โค 1000
- 1 โค wordDict[i].length โค 10
- s and wordDict[i] consist of only lowercase English letters
- All the strings of wordDict are unique
- Input is guaranteed to be valid according to the problem's requirements
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code