lc-go/solutions/1/q139/solution.go
2026-01-17 12:51:40 +09:00

28 lines
454 B
Go

package q139
func wordBreak(s string, wordDict []string) bool {
possible := make([]bool, len(s)+1)
possible[0] = true
for l := range possible {
if !possible[l] {
continue
}
for i := range wordDict {
newLen := len(wordDict[i]) + l
if newLen > len(s) {
continue
}
if possible[newLen] {
continue
}
if s[l:newLen] == wordDict[i] {
possible[newLen] = true
}
}
}
return possible[len(s)]
}
var _ = wordBreak