Design Gurus Logo
Word Break (medium)
Go Back

Problem Statement

Given a non-empty string and a dictionary containing a list of non-empty words, determine if the string can be segmented into a space-separated sequence of one or more dictionary words. Each word in the dictionary can be reused multiple times.

Examples

Example 1:

  • Input:
    • String: "ilovecoding"
    • Dictionary: ["i", "love", "coding"]
  • Expected Output: True
  • Justification: The string can be segmented as "i love coding".

Example 2:

  • Input:
    • String: "helloworld"
    • Dictionary: ["hello", "world", "hell", "low"]
  • Expected Output: True
  • Justification: The string can be segmented as "hello world".

Example 3:

  • Input:
    • String: "enjoylife"
    • Dictionary: ["enj", "life", "joy"]
  • Expected Output: False
  • Justification: Despite having the words "enj" and "life" in the dictionary, we can't segment the string into the space-separated dictionary words.

Constraints:

  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20
  • s and wordDict[i] consist of only lowercase English letters.
  • All the strings of wordDict are unique.

Try it yourself

Try solving this question here:

Python3
Python3