We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ce42aaa commit 6a37193Copy full SHA for 6a37193
word-break/uraflower.js
@@ -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