Building upon "Word Break," this version asks you to return all possible sentences that can be formed by adding spaces in the string s such that each word is a valid dictionary word.
This "Hard" problem is popular at Uber and Microsoft. It combines Dynamic Programming (to check for feasibility) with Backtracking (to reconstruct the paths). It tests a candidate's ability to manage complex return types (lists of strings) and efficient memory usage via memoization.
The pattern is Backtracking with Memoization. You define a function that returns all possible sentence completions for a given suffix of the string.
String: catsanddog, Dictionary: ["cat", "cats", "and", "sand", "dog"].
cat + sanddog -> cat + sand + dog -> "cat sand dog"cats + anddog -> cats + and + dog -> "cats and dog"
Result: ["cat sand dog", "cats and dog"].Understand that while DP tells you if a solution exists, Backtracking allows you to find the solution. Combining them is a common pattern for "Return all possible..." type questions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Word Break | Medium | Solve | |
| Stickers to Spell Word | Hard | Solve | |
| Extra Characters in a String | Medium | Solve | |
| Palindrome Pairs | Hard | Solve | |
| Word Squares | Hard | Solve |