forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-break-ii.go
executable file
·72 lines (57 loc) · 1.33 KB
/
word-break-ii.go
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
71
72
package problem0140
import "sort"
func wordBreak(s string, wordDict []string) []string {
if len(wordDict) == 0 {
return []string{}
}
// dict 方便查找 wordDict 中的单词
dict := make(map[string]bool, len(wordDict))
length := make(map[int]bool, len(wordDict))
for _, w := range wordDict {
dict[w] = true
length[len(w)] = true
}
// sizes 是 wordDict 中单词长度的集合
// sizes 可以加快 for 循环
sizes := make([]int, 0, len(length))
for k := range length {
sizes = append(sizes, k)
}
sort.Ints(sizes)
n := len(s)
// dp[i] 等于 len(wordBreak(s[:i+1], wordDict))
dp := make([]float64, len(s)+1)
dp[0] = 1
for i := 0; i <= n; i++ {
if dp[i] == 0 {
continue
}
for _, size := range sizes {
if i+size <= n && dict[s[i:i+size]] {
dp[i+size] += dp[i]
}
}
}
// 利用 dp[n] 统计解的数量,可以避免无用功
// 取消下一行的注释符号,看看各个 test case 的 dp
// fmt.Println(dp)
if dp[n] == 0 {
return []string{}
}
res := make([]string, 0, int(dp[n]))
// 利用 dfs 获取所有的解
var dfs func(int, string)
dfs = func(i int, str string) {
if i == len(s) {
res = append(res, str[1:])
return
}
for _, size := range sizes {
if i+size <= len(s) && dict[s[i:i+size]] {
dfs(i+size, str+" "+s[i:i+size])
}
}
}
dfs(0, "")
return res
}