Skip to content

Commit 1df0a9a

Browse files
author
jinvicky
committed
word break solution
1 parent 27a0546 commit 1df0a9a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

word-break/jinvicky.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean wordBreak(String s, List<String> wordDict) {
5+
int n = s.length();
6+
boolean[] dp = new boolean[n + 1];
7+
dp[0] = true; // 빈 문자열은 항상 가능
8+
9+
for (int i = 0; i < n; i++) {
10+
if (!dp[i]) continue; // 여기까지 못 오면 확장 불가
11+
for (String w : wordDict) {
12+
int j = i + w.length();
13+
if (j <= n && s.startsWith(w, i)) {
14+
dp[j] = true;
15+
}
16+
}
17+
}
18+
return dp[n];
19+
}
20+
}

0 commit comments

Comments
 (0)