Skip to content
Decoder

Word Break

Medium

Problem

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.

Example 1

Input
s = "pineapple", word_dict = ["pine", "apple", "pen"]
Output
true

Example 2

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

Example 3

Input
s = "aaaa", word_dict = ["aa"]
Output
true
Why
"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.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.