Word Break

MediumDynamic Programming I: 1D

You are given a string s and a list of words wordDict. Return true if s can be cut into a sequence of one or more pieces, where every piece is a word from wordDict. Otherwise return false.

The pieces must cover s completely and in order, with nothing left over. The same word may be used any number of times, and words you don't need can be ignored.

Constraints

  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20
  • s and every word contain only lowercase English letters; the words are all different.

Examples

Example 1

Input: s = "hotdogstand", wordDict = ["hot","dog","stand","hotdog","sta"]
Output: true

hot + dog + stand works (so does hotdog + stand).

Example 2

Input: s = "carpets", wordDict = ["car","pet","carp"]
Output: false

car + pet leaves an s, and carp leaves ets. No split uses only dictionary words.

Example 3

Input: s = "abab", wordDict = ["ab"]
Output: true

A word may be used more than once: ab + ab.

JavaScript you'll use: Strings and characters Map and Set

/**
 * @param {string} s
 * @param {string[]} wordDict
 * @return {boolean}
 */
function wordBreak(s, wordDict) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.