Skip to content

Commit 1e91ae8

Browse files
committed
[Week5](gmlwls96) Word Break.
1 parent f665acb commit 1e91ae8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

word-break/gmlwls96.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
* 시간 : O(s^2*w), 공간 : O(s)
4+
* */
5+
fun wordBreak(s: String, wordDict: List<String>): Boolean {
6+
val dp = BooleanArray(s.length + 1)
7+
dp[0] = true
8+
for (i in 1..s.length) {
9+
val subS = s.substring(0, i)
10+
val endWord = wordDict.firstOrNull { subS.endsWith(it) }
11+
if (endWord != null) {
12+
dp[i] = dp[i - endWord.length]
13+
}
14+
}
15+
return dp[s.length]
16+
}
17+
}

0 commit comments

Comments
 (0)