Skip to content

Commit

Permalink
add js solution for wordBreak
Browse files Browse the repository at this point in the history
  • Loading branch information
jackeyjia authored Jul 8, 2021
1 parent 97636cc commit 8f7353c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions problems/0139.单词拆分.md
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
```



-----------------------
Expand Down

0 comments on commit 8f7353c

Please sign in to comment.