-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode1202.go
69 lines (60 loc) · 1.18 KB
/
LeetCode1202.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
package main
import (
"fmt"
"sort"
)
type DisjSets struct {
data []int
}
func InitDisj(n int) *DisjSets {
d := new(DisjSets)
d.data = make([]int, n)
for i := 0; i < n; i++ {
d.data[i] = i
}
return d
}
func (self *DisjSets) Union(i1, i2 int) {
f1, f2 := self.Find(i1), self.Find(i2)
if f1 == f2 {
return
}
self.data[f2] = f1
}
func (self *DisjSets) Find(i int) (st int) {
if self.data[i] == i {
return i
}
self.data[i] = self.Find(self.data[i])
return self.data[i]
}
func (self *DisjSets) GroupBy() map[int][]int {
res := make(map[int][]int)
for i, v := range self.data {
res[self.Find(v)] = append(res[self.Find(v)], i)
}
return res
}
func smallestStringWithSwaps(s string, pairs [][]int) string {
ls := len(s)
newS := []byte(s)
disj := InitDisj(ls)
for i := 0; i < len(pairs); i++ {
disj.Union(pairs[i][0], pairs[i][1])
}
m := disj.GroupBy()
for _ , ts := range m {
t := make([]int, len(ts))
copy(t, ts)
sort.Slice(t, func(i, j int) bool {
return s[t[i]] < s[t[j]]
})
for i := 0; i < len(t); i++ {
newS[ts[i]] = s[t[i]]
}
}
return string(newS)
}
func main() {
fmt.Println(smallestStringWithSwaps("dcab" , [][]int{{0,3},{1,2},{0,2}}))
}