forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0139-word-break.kt
More file actions
17 lines (17 loc) · 835 Bytes
/
0139-word-break.kt
File metadata and controls
17 lines (17 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
fun wordBreak(s: String, wordDict: List<String>): Boolean {
val cache = BooleanArray(s.length+1)
cache[s.length] = true // lastPos is true, meaning [array of word length], lastPos] since string "" can be segmented of any list of any words (our dp basecase)
for(i in s.length-1 downTo 0){
for(word in wordDict){
if(word.length+i <= s.length){//enough chars in s for this word to be viable ?
if(word == s.substring(i, i+word.length)){// its the same word char by char?
if(cache[i+word.length] == true) //this cache index is true only if the cache at index [i+word.length is true]
cache[i] = true
}
}
}
}
return cache[0]
}
}