Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions basic_algorithm/dp.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,32 +486,37 @@ func wordBreak(s string, wordDict []string) bool {
}
f := make([]bool, len(s)+1)
f[0] = true
max := maxLen(wordDict)
max,dict := maxLen(wordDict)
for i := 1; i <= len(s); i++ {
for j := i - max; j < i && j >= 0; j++ {
if f[j] && inDict(s[j:i]) {
l := 0
if i - max > 0 {
l = i - max
}
for j := l; j < i; j++ {
if f[j] && inDict(s[j:i],dict) {
f[i] = true
break
break
}
}
}
return f[len(s)]
}

var dict = make(map[string]bool)

func maxLen(wordDict []string) int {

func maxLen(wordDict []string) (int,map[string]bool) {
dict := make(map[string]bool)
max := 0
for _, v := range wordDict {
dict[v] = true
if len(v) > max {
max = len(v)
}
}
return max
return max,dict
}

func inDict(s string) bool {
func inDict(s string,dict map[string]bool) bool {
_, ok := dict[s]
return ok
}
Expand Down