-
Notifications
You must be signed in to change notification settings - Fork 481
/
1202.go
41 lines (36 loc) · 850 Bytes
/
1202.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
var p []int
func smallestStringWithSwaps(s string, pairs [][]int) string {
p = make([]int, len(s))
for i := 0; i < len(s); i++ {
p[i] = i
}
for _, v := range pairs {
px, py := find(v[0]), find(v[1])
if px != py {
p[px] = py
}
}
mem := make(map[int][]byte)
for i := 0; i < len(p); i++ {
x := find(p[i])
mem[x] = append(mem[x], s[i])
}
for k, _ := range mem {
sort.Slice(mem[k], func(i, j int) bool {
return mem[k][i] < mem[k][j]
})
}
res := make([]byte, 0)
for i := 0; i < len(s); i++ {
x := find(i)
res = append(res, mem[x][0])
mem[x] = mem[x][1:]
}
return string(res[:])
}
func find(x int) int {
if x != p[x] {
p[x] = find(p[x])
}
return p[x]
}