Skip to main content

Word Break

Problem statement

Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.

Note that the same word in the dictionary may be reused multiple times in the segmentation.

Example 1:

Input: s = "leetcode", wordDict = ["leet","code"]Output: trueExplanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple","pen"]Output: trueExplanation: Return true because "applepenapple" can be segmented as "apple pen apple".Note that you are allowed to reuse a dictionary word.

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
  • s and wordDict[i] consist of only lowercase English letters.
  • All the strings of wordDict are unique.

My solution

/**
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = function(s, wordDict) {
const dict = new Set(wordDict);
const queue = [0];
const visited = Array.from({
length: s.length
}, () => false)

while (queue.length) {
const start = queue.shift();

if (visited[start]) {
continue
}

for (let end = start + 1; end <= s.length; end++) {
const str = s.slice(start, end);

if (!dict.has(str)) {
continue
}

if (end === s.length) {
return true;
}
queue.push(end)
}

visited[start] = true;
}
return false
};