Skip to content

Commit abcb7d3

Browse files
committed
word-break solution
1 parent d676e73 commit abcb7d3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

word-break/krokerdile.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} wordDict
4+
* @return {boolean}
5+
*/
6+
var wordBreak = function(s, wordDict) {
7+
const wordSet = new Set(wordDict); // 빠른 검색을 위한 Set
8+
const dp = Array(s.length + 1).fill(false);
9+
dp[0] = true; // 빈 문자열은 항상 가능
10+
11+
for (let i = 1; i <= s.length; i++) {
12+
for (let j = 0; j < i; j++) {
13+
const word = s.slice(j, i);
14+
if (dp[j] && wordSet.has(word)) {
15+
dp[i] = true;
16+
break; // 더 이상 볼 필요 없음
17+
}
18+
}
19+
}
20+
21+
return dp[s.length];
22+
};

0 commit comments

Comments
 (0)