-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp140.go
More file actions
70 lines (55 loc) · 1.62 KB
/
Copy pathp140.go
File metadata and controls
70 lines (55 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"strings"
)
func wordBreak(s string, wordDict []string) []string {
if len(s) == 0 {
return []string{}
}
alphabets := make(map[rune]bool)
for _, w := range wordDict {
for _, c := range w {
alphabets[c] = true
}
}
for _, c := range s {
if _, ok := alphabets[c]; !ok {
return []string{}
}
}
var record = make(map[string][]string)
return wordBreak2(s, wordDict, &record)
}
func wordBreak2(s string, wordDict []string, rDict *map[string][]string) []string {
res := make([]string, 0)
for _, w := range wordDict {
head := strings.Index(s, w)
if head == 0 {
_s := s[len(w):]
if len(_s) == 0 {
res = append(res, w)
} else {
if _r, ok := (*rDict)[_s]; !ok {
_r = wordBreak2(_s, wordDict, rDict)
for _, rs := range _r {
res = append(res, fmt.Sprintf("%s %s", w, rs))
}
} else {
for _, rs := range _r {
res = append(res, fmt.Sprintf("%s %s", w, rs))
}
}
}
}
}
(*rDict)[s] = res
return res
}
func main() {
fmt.Println(wordBreak("aaaaaaa", []string{"aaaa", "aaa"}))
// fmt.Println(wordBreak("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", []string{"aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa", "ba"}))
fmt.Println(wordBreak("catsanddog", []string{"cat", "cats", "and", "sand", "dog"}))
fmt.Println(wordBreak("pineapplepenapple", []string{"apple", "pen", "applepen", "pine", "pineapple"}))
fmt.Println(wordBreak("catsandog", []string{"cats", "dog", "sand", "and", "cat"}))
}