mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #477 from jackeyjia/patch-7
add js solution for wordBreak
This commit is contained in:
@ -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];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user