Skip to content

Commit 1f58190

Browse files
committed
add solution: word-break
1 parent c6260ae commit 1f58190

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

word-break/yihyun-kim1.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} wordDict
4+
* @return {boolean}
5+
*/
6+
const wordBreak = (s, wordDict) => {
7+
const dp = new Array(s.length + 1).fill(false);
8+
dp[0] = true;
9+
10+
for (let i = 1; i <= s.length; i++) {
11+
for (const word of wordDict) {
12+
const start = i - word.length;
13+
if (start >= 0 && dp[start] && s.slice(start, i) === word) {
14+
dp[i] = true;
15+
}
16+
}
17+
}
18+
19+
return dp[s.length];
20+
};

0 commit comments

Comments
 (0)