Created
April 15, 2020 22:45
-
-
Save mrboli/912bf9a0828760e13390d94bc22eeba6 to your computer and use it in GitHub Desktop.
Word Break
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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