Skip to content

Commit 2c831f6

Browse files
committed
Word Break
1 parent 04a6a59 commit 2c831f6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

word-break/casentino.ts

Lines changed: 15 additions & 0 deletions
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[0] = true;
4+
for (let i = 1; i <= s.length; i++) {
5+
let str = "";
6+
for (let j = 0; j < wordDict.length; j++) {
7+
let start = i - wordDict[j].length;
8+
if (start >= 0 && dp[start] && s.substring(start, i) === wordDict[j]) {
9+
dp[i] = true;
10+
break;
11+
}
12+
}
13+
}
14+
return dp[s.length];
15+
}

0 commit comments

Comments
 (0)