Word Break

Dynamic Programming, problem 6 of 9

Word Break

Medium

LC #139

string dphash set1d dp

Not attempted yet

Return True if s can be split into a sequence of one or more words from word_dict. A word may be used any number of times.

Example 1

Input: s = "leetcode", word_dict = ["leet", "code"]
Output: True

Example 2

Input: s = "applepenapple",
       word_dict = ["apple", "pen"]
Output: True
"apple" is used twice

Example 3

Input: s = "catsandog",
       word_dict = ["cats", "dog", "sand", "and", "cat"]
Output: False

Constraints

  • 1 ≤ len(s) ≤ 300
  • 1 ≤ len(word_dict) ≤ 1000, words are 1–20 letters
  • lowercase letters only; the words are distinct

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.