diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index b6a6242e..59892ef9 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -291,6 +291,27 @@ func wordBreak(s string,wordDict []string) bool { } ``` +Javascript: +```javascript +const wordBreak = (s, wordDict) => { + + let dp = Array(s.length + 1).fill(false); + dp[0] = true; + + for(let i = 0; i <= s.length; i++){ + for(let j = 0; j < wordDict.length; j++) { + if(i >= wordDict[j].length) { + if(s.slice(i - wordDict[j].length, i) === wordDict[j] && dp[i - wordDict[j].length]) { + dp[i] = true + } + } + } + } + + return dp[s.length]; +} +``` + -----------------------