Skip to content

Commit 7bffe74

Browse files
week11 mission word-break
1 parent 38fa297 commit 7bffe74

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

word-break/dev-jonghoonpark.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
- 문제: https://leetcode.com/problems/word-break/
2+
- 풀이: https://algorithm.jonghoonpark.com/2024/02/28/leetcode-139
3+
4+
```java
5+
class Solution {
6+
public boolean wordBreak(String s, List<String> wordDict) {
7+
boolean[] dp = new boolean[s.length() + 1];
8+
dp[0] = true;
9+
10+
for (int i = 1; i <= s.length(); i++) {
11+
for (String word : wordDict) {
12+
if (i >= word.length()) {
13+
int start = i - word.length();
14+
if (dp[start] && s.startsWith(word, start)) {
15+
dp[i] = true;
16+
}
17+
}
18+
}
19+
}
20+
21+
return dp[s.length()];
22+
}
23+
}
24+
```
25+
26+
### TC, SC
27+
28+
s의 길이를 n 이라 하고, wordDict의 크기를 m 이라고 할 때, 시간복잡도는 `O(n * m)` 공간복잡도는 `O(n)` 이다.

0 commit comments

Comments
 (0)