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
// @Title: 交换字符串中的元素 (Smallest String With Swaps)
// @Author: 15816537946@163.com
// @Date: 2021-01-11 23:48:24
// @Runtime: 116 ms
// @Memory: 21.6 MB
func smallestStringWithSwaps(s string, pairs [][]int) string {
	n := len(s)
	uf := newUnionFind(n)

	for _, p := range pairs {
		uf.connect(p[0], p[1])
	}

	groups := make(map[int][]int, n)
	for c, p := range uf.parent {
		p = uf.find(p)
		// 相连的索引值,全部放在一起
		groups[p] = append(groups[p], c)
	}

	bytes := []byte(s)
	res := make([]byte, n)
	for _, g := range groups {
		size := len(g)
		a := make([]int, size)
		copy(a, g)
		// a 中的索引值,按照其在 bytes 中对应的字符大小来排序
		sort.Slice(a, func(i, j int) bool {
			return bytes[a[i]] < bytes[a[j]]
		})
		// g 中的索引值,按照其自身的大小排序
		sort.Ints(g)
		// 越小的位置,放入的值也越小
		for i := 0; i < size; i++ {
			res[g[i]] = bytes[a[i]]
		}
	}

	return string(res)
}

type unionFind struct {
	parent []int
}

func newUnionFind(size int) *unionFind {
	parent := make([]int, size)
	for i := range parent {
		parent[i] = i
	}
	return &unionFind{
		parent: parent,
	}
}

func (uf *unionFind) connect(x, y int) {
	uf.parent[uf.find(x)] = uf.find(y)
}

func (uf *unionFind) find(i int) int {
	if uf.parent[i] != i {
		uf.parent[i] = uf.find(uf.parent[i])
	}
	return uf.parent[i]
}