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 e82a5cc commit 8787193Copy full SHA for 8787193
word-break/sukyoungshin.ts
@@ -0,0 +1,23 @@
1
+function wordBreak(s: string, wordDict: string[]): boolean {
2
+ const cache: Record<string, boolean> = {};
3
+
4
+ function canSplit(str: string): boolean {
5
+ if (str === "") return true;
6
+ if (cache[str] !== undefined) return cache[str];
7
8
+ for (let i = 1; i <= str.length; i++) {
9
+ const left = str.slice(0, i);
10
+ const right = str.slice(i);
11
12
+ if (wordDict.includes(left) && canSplit(right)) {
13
+ cache[str] = true;
14
+ return true;
15
+ }
16
17
18
+ cache[str] = false;
19
+ return false;
20
21
22
+ return canSplit(s);
23
+};
0 commit comments