/**
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
let status = {
done: false,
};
var wordBreak = function(s, wordDict) {
status = {
done: false,
flag: false,
};
validate(s, wordDict);
return status.flag;
};
function validate(s, wordDict) {
if (status.flag === true) {
return;
}
if (s.trim().length === 0) {
status.flag = true;
return;
}
for (let i = 0; i < wordDict.length; i++) {
const currWord = wordDict[i];
const regex = new RegExp(currWord, '');
if (s.includes(currWord)) {
const newStr = s.replace(regex, ' ')
validate(newStr, wordDict);
}
}
}