lc-go/solutions/1/q139/solution.go

29 lines
538 B
Go

// Package q139 implements a solution for https://leetcode.com/problems/word-break/
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