Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2529897

Browse files
Time: 4 ms (94.21%), Space: 42.2 MB (51.32%) - LeetHub
1 parent bc2973c commit 2529897

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎0139-word-break/0139-word-break.kt‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
fun wordBreak(s: String, wordDict: List<String>): Boolean {
3+
val n = s.length
4+
val isPossible = BooleanArray(n)
5+
isPossible[0] = true
6+
7+
for (i in 0 until n) {
8+
if (!isPossible[i]) {
9+
continue
10+
}
11+
12+
for (word in wordDict) {
13+
val end = i + word.length
14+
if (end > n) {
15+
continue
16+
}
17+
if (s.substring(i, end) != word) {
18+
continue
19+
}
20+
21+
if (end == n) {
22+
return true
23+
}
24+
isPossible[end] = true
25+
}
26+
}
27+
28+
return false
29+
}
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /