3
* @param {string[]} wordDict6
var wordBreak = function (s, wordDict) {7
var dp = new Array(s.length + 1).fill(false);10
for (var i = s.length - 1; i >= 0; i--) {11
for (const word of wordDict) {13
i + word.length <= s.length &&14
s.substring(i, i + word.length) === word16
dp[i] = dp[i + word.length];26
// naive approach, take each word from the set and check if they match28
// considering m as the dictionary size31
for (const word of wordDict) {35
return canSegment(s, dict, 0);37
function canSegment (str, dict, index) {38
if (index >= str.length) return true;41
for (const word of dict.values()) {43
if ((index + word.length) <= str.length) {44
var substring = str.substring(index, index + word.length);46
if (dict.has(substring)) {47
success = success | canSegment(str, dict, index + word.length);