Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Word Break - All Possible Sentences



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Word Break - All Sentences

Word Break - All Sentences

Problem Statement

Given a non-empty string text and a list of non-empty words called dictList, insert spaces into text to form sentences where each word is contained in dictList. Return all such possible sentences.

Note:

  • You can reuse the same word from the dictionary multiple times in the segmentation.
  • The dictionary is guaranteed to have no repeated words.

Examples

text = "dogsandcat"
dictList = ["dog", "dogs", "and", "cat"]
Output:
[
  "dogs and cat",
  "dog sand cat"
]
text = "applepenapple"
dictList = ["apple", "pen", "applepen"]
Output:
[
  "apple pen apple",
  "applepen apple"
]
text = "catsandbat"
dictList = ["cats", "dog", "sand", "and", "cat", "bat"]
Output:
[
  "cats and bat",
  "cat sand bat"
]

Constraints

  • 1 ≤ length of text ≤ 25
  • 1 ≤ number of words in dictList ≤ 20
  • All dictionary words and text consist of lowercase English letters only.