Blind 75
57 / 75Free practice

Word Break

Medium · Dynamic programming

Jump to code

Decide whether a string can be split into one or more words from a dictionary of nonempty strings. Dictionary words can be reused. Treat an empty input as segmentable.

Examples

Example 1

s = "leetcode"
wordDict = ["leet","code"]

Output: true

Example 2

s = "applepenapple"
wordDict = ["apple","pen"]

Output: true

Constraints

  • 1 <= s.length <= 200
  • 1 <= wordDict.length <= 100
  • 1 <= wordDict[i].length <= 20
  • s and wordDict[i] consist of only lowercase English letters.
Need a hint?

Ask whether each prefix can end with a dictionary word.

Asked in real interviews

Candidates reported versions of Word Break from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 7 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  "leetcode",
  [
    "leet",
    "code"
  ]
]
Expected
true
Case 2
Input
[
  "applepenapple",
  [
    "apple",
    "pen"
  ]
]
Expected
true
Case 3
Input
[
  "catsandog",
  [
    "cats",
    "dog",
    "sand",
    "and",
    "cat"
  ]
]
Expected
false
Case 4
Input
[
  "aaaaaaa",
  [
    "aaaa",
    "aaa"
  ]
]
Expected
true
Case 5
Input
[
  "abcd",
  [
    "a",
    "abc",
    "b"
  ]
]
Expected
false

Drafts are saved per language in this browser when storage is available.