Skip to content

Word Break

MediumDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

Return true if the string s can be split into a sequence of one or more words from word_dict. Words may be reused any number of times.

s = "pineapple", word_dict = ["pine", "apple", "pen"]               →  true
s = "catsandog", word_dict = ["cats", "dog", "sand", "and", "cat"]  →  false
s = "aaaa",      word_dict = ["aa"]                                 →  true   ("aa" twice)

An empty string counts as breakable.

Constraints

  • 0 <= s.length <= 300
  • 1 <= word_dict.length <= 1000
  • 1 <= word_dict[i].length <= 20
  • s and every word contain only lowercase letters.