Skip to content

Commit 40601e8

Browse files
committed
word break solution
1 parent 0ac9b42 commit 40601e8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

word-break/hyunolike.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean wordBreak(String s, List<String> wordDict) {
3+
int n = s.length();
4+
boolean[] dp = new boolean[n+1];
5+
dp[0] = true;
6+
7+
Set<String> wordSet = new HashSet<>(wordDict);
8+
9+
for(int i = 1; i <= n; i++) {
10+
for(int j = 0; j < i; j++) {
11+
if(dp[j] && wordSet.contains(s.substring(j, i))) {
12+
dp[i] = true;
13+
break;
14+
}
15+
}
16+
}
17+
return dp[n];
18+
}
19+
}

word-break/hyunolike.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function wordBreak(s: string, wordDict: string[]): boolean {
2+
const n = s.length;
3+
const dp:boolean[] = Array(n+1).fill(false);
4+
dp[0] = true;
5+
6+
const wordSet = new Set(wordDict);
7+
8+
for(let i = 1; i <= n; i++) {
9+
for(let j = 0; j < i; j++) {
10+
if(dp[j] && wordSet.has(s.substring(j, i))) {
11+
dp[i] = true;
12+
break;
13+
}
14+
}
15+
}
16+
return dp[n];
17+
};

0 commit comments

Comments
 (0)