Skip to content

Commit 742008b

Browse files
committed
feat(soobing): word-break
1 parent eaa4ece commit 742008b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

word-break/soobing.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function wordBreak(s: string, wordDict: string[]): boolean {
2+
const dp = new Array(s.length + 1).fill(false);
3+
dp[s.length] = true;
4+
5+
for (let i = s.length - 1; i >= 0; i--) {
6+
for (const word of wordDict) {
7+
if (i + word.length <= s.length && s.slice(i, i + word.length) === word) {
8+
dp[i] = dp[i + word.length];
9+
}
10+
11+
if (dp[i]) break;
12+
}
13+
}
14+
return dp[0];
15+
}

0 commit comments

Comments
 (0)