Skip to content

Commit b5fc3ec

Browse files
committed
word break solution
- using set
1 parent 064d73a commit b5fc3ec

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

word-break/hyer0705.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
// using set
2+
function wordBreak(s: string, wordDict: string[]): boolean {
3+
const sLen = s.length;
4+
5+
const dp: boolean[] = new Array(sLen + 1).fill(false);
6+
dp[0] = true;
7+
8+
const wordSet = new Set<string>(wordDict);
9+
10+
for (let i = 1; i <= sLen; i++) {
11+
for (let j = 0; j < i; j++) {
12+
if (dp[j] && wordSet.has(s.substring(j, i))) {
13+
dp[i] = true;
14+
break;
15+
}
16+
}
17+
}
18+
19+
return dp[sLen];
20+
}
21+
22+
// using trie
123
class TNode {
224
isEndOf: boolean;
325
children: Map<string, TNode>;

0 commit comments

Comments
 (0)