Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given

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

A solution is

  "cat sand dog", 
  "cats and dog"

Make sure the strings are sorted in your result.

NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Word Break II on Interview Code Editor
Sign Up
to access hints and editorial solutions for Word Break II
Asked In:


Click here to start solving coding interview questions