Skip to content

Instantly share code, notes, and snippets.

@mrboli
Created April 15, 2020 22:45
Show Gist options
  • Save mrboli/912bf9a0828760e13390d94bc22eeba6 to your computer and use it in GitHub Desktop.
Save mrboli/912bf9a0828760e13390d94bc22eeba6 to your computer and use it in GitHub Desktop.
Word Break
var wordBreak = function(s, wordDict) {
let dict = new Set(wordDict);
let isValid = Array(s.length + 1).fill(false);
isValid[0] = true;
for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (isValid[j] && dict.has(s.substring(j, i))) {
isValid[i] = true;
break;
}
}
}
return isValid[s.length];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment