Problem Description
Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. Words in the dictionary may be reused multiple times.
Key Insights
- Use backtracking (DFS) to try splitting the string at every possible index.
- Utilize memoization to store the list of valid sentences starting from a given index to avoid redundant computations.
- The problem is naturally solved through recursion with memoization (a top-down dynamic programming approach).
Space and Time Complexity
Time Complexity: O(n^2 * m) in the worst case, where n is the length of the string and m is the average number of words found per recursion; memoization helps to reduce repeated work. Space Complexity: O(n * m) for the recursion stack and memoization storage, where m represents the average length of the sentence list from each index.
Solution
We use a depth-first search (DFS) that tries every possible break point in the string. At each index, if the current substring is in the dictionary, we recursively solve for the remainder of the string. Memoization is used to cache results for each index to avoid redundant computations. The solution builds the final sentences by concatenating the current word with the sentence fragments returned by the recursive call.