Skip to content

Word Break II

HardBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

Given a string s and a list of dictionary words wordDict, add spaces to s so that every resulting word is in the dictionary. Return all such sentences, in any order. A dictionary word may be reused any number of times.

s = "catsanddog", wordDict = ["cat", "cats", "and", "sand", "dog"]
→  ["cats and dog", "cat sand dog"]
 
s = "catsandog",  wordDict = ["cats", "dog", "sand", "and", "cat"]
→  []

Constraints

  • 1 <= s.length <= 20
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 10
  • All the words in wordDict are unique.