Skip to content
Onsite

Word Break

MediumP1Coding

Given a string s and a list of words wordDict, return true if s can be split into a sequence of one or more dictionary words (a word may be used more than once), otherwise false.

Example 1

Input
s = "leetcode", wordDict = ["leet","code"]
Output
true

Example 2

Input
s = "applepenapple", wordDict = ["apple","pen"]
Output
true
Why
"apple" is reused.

Example 3

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

Constraints

  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20
  • All strings are lower-case letters and the words are distinct.

Hints

Run checks the 3 visible tests, Submit all 11.