Skip to content

Commit 5b5c64a

Browse files
authored
Update 139-word-break.js
1 parent 5b3180c commit 5b5c64a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

139-word-break.js

+25
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,28 @@ const wordBreak = function(s, wordDict) {
5151

5252
return f[len];
5353
};
54+
55+
56+
// another
57+
58+
/**
59+
* @param {string} s
60+
* @param {string[]} wordDict
61+
* @return {boolean}
62+
*/
63+
const wordBreak = function(s, wordDict) {
64+
const set = new Set(wordDict)
65+
const dp = Array(s.length + 1).fill(false)
66+
dp[0] = true
67+
for(let i = 1; i <= s.length; i++) {
68+
for(let j = 0; j < i; j++) {
69+
if(dp[j] && set.has(s.slice(j, i))) {
70+
dp[i] = true
71+
break
72+
}
73+
}
74+
}
75+
76+
return dp[s.length]
77+
};
78+

0 commit comments

Comments
 (0)