Word Break II

Given anon-emptystring_s_and a dictionary_wordDict_containing a list ofnon-emptywords, add spaces in_s_to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.

Note:

  • The same word in the dictionary may be reused multiple times in the segmentation.

  • You may assume the dictionary does not contain duplicate words.

Example 1:

Input:

s = "
catsanddog
"
wordDict = 
["cat", "cats", "and", "sand", "dog"]
Output:

[
  "cats and dog",
  "cat sand dog"
]

Example 2:

Example 3:

Solution

JAVA solution based on memorized DFS

Modified version:

https://leetcode.com/problems/word-break-ii/discuss/44167/My-concise-JAVA-solution-based-on-memorized-DFS

Last updated

Was this helpful?