-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathword_break.go
77 lines (71 loc) · 1.31 KB
/
word_break.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
73
74
75
76
77
package main
type DisjointSet struct {
roots []int
rank []int
}
func NewDisjointSet(n int) *DisjointSet {
ds := &DisjointSet{
roots: make([]int, n),
rank: make([]int, n),
}
for i := 0; i < n; i++ {
ds.roots[i] = i
ds.rank[i] = 0
}
return ds
}
func (ds *DisjointSet) find(x int) int {
if ds.roots[x] == x {
return x
}
s := []int{}
for ds.roots[x] != x {
s = append(s, x)
x = ds.roots[x]
}
for len(s) > 0 {
ds.roots[s[len(s)-1]] = x
s = s[:len(s)-1]
}
return x
}
func (ds *DisjointSet) isConnected(x, y int) bool {
return ds.find(x) == ds.find(y)
}
func (ds *DisjointSet) union(x, y int) {
rX := ds.find(x)
rY := ds.find(y)
if rX == rY {
return
}
if ds.rank[rX] > ds.rank[rY] {
ds.roots[rY] = rX
} else if ds.rank[rX] < ds.rank[rY] {
ds.roots[rX] = rY
} else {
ds.roots[rY] = rX
ds.rank[rX]++
}
}
func wordBreak(s string, wordDict []string) bool {
disjointSet := NewDisjointSet(len(s) + 1)
for i := 0; i < len(wordDict); i++ {
for j := 0; j < len(s); j++ {
isValid := true
for k := 0; k < len(wordDict[i]); k++ {
if j+k >= len(s) {
isValid = false
break
}
if wordDict[i][k] != s[j+k] {
isValid = false
break
}
}
if isValid {
disjointSet.union(j, j+len(wordDict[i]))
}
}
}
return disjointSet.isConnected(0, len(s))
}