File tree Expand file tree Collapse file tree 1 file changed +8
-7
lines changed
Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change @@ -486,36 +486,37 @@ func wordBreak(s string, wordDict []string) bool {
486486}
487487f := make ([]bool , len (s)+1 )
488488f[0 ] = true
489- max := maxLen (wordDict)
489+ max, dict := maxLen (wordDict)
490490for i := 1 ; i <= len (s); i++ {
491491l := 0
492492if i - max > 0 {
493493l = i - max
494494}
495495for j := l; j < i; j++ {
496- if f[j] && inDict (s[j:i]) {
496+ if f[j] && inDict (s[j:i],dict ) {
497497f[i] = true
498- break
498+ break
499499}
500500}
501501}
502502return f[len (s)]
503503}
504504
505- var dict = make (map [string ]bool )
506505
507- func maxLen (wordDict []string ) int {
506+
507+ func maxLen (wordDict []string ) (int ,map [string ]bool ) {
508+ dict := make (map [string ]bool )
508509max := 0
509510for _ , v := range wordDict {
510511dict[v] = true
511512if len (v) > max {
512513max = len (v)
513514}
514515}
515- return max
516+ return max,dict
516517}
517518
518- func inDict (s string ) bool {
519+ func inDict (s string , dict map [ string ] bool ) bool {
519520_ , ok := dict[s]
520521return ok
521522}
You can’t perform that action at this time.
0 commit comments