Skip to content

Commit 6a37193

Browse files
authored
[ PS ] : Word Break
1 parent ce42aaa commit 6a37193

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

word-break/uraflower.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} wordDict
4+
* @return {boolean}
5+
*/
6+
const wordBreak = function (s, wordDict) {
7+
const dp = Array(s.length);
8+
const dict = new Set(wordDict);
9+
10+
function recurse(start) {
11+
if (start === s.length) return true;
12+
if (dp[start] !== undefined) return dp[start];
13+
14+
for (let end = start + 1; end <= s.length; end++) {
15+
const substr = s.slice(start, end);
16+
if (dict.has(substr) && recurse(end)) {
17+
dp[start] = true;
18+
return true;
19+
}
20+
}
21+
22+
dp[start] = false;
23+
return false;
24+
}
25+
26+
return recurse(0);
27+
};
28+
29+
// 시간복잡도: O(n^2) (n: s.length. n번 재귀 & 최대 n번 슬라이싱)
30+
// 공간복잡도: O(n + m) (m: wordDict.length)

0 commit comments

Comments
 (0)