Skip to content
Decoder

Word Break II

Hard

Problem

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.

Example 1

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

Example 2

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

Example 3

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

Example 4

→  []

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.