Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 89781c9

Browse files
committed
1202 accepted. 124 ms, faster than 61.54%
1 parent 04ef7d4 commit 89781c9

File tree

4 files changed

+204
-46
lines changed

4 files changed

+204
-46
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [1202. Smallest String With Swaps](https://leetcode.com/problems/smallest-string-with-swaps/)
2+
3+
You are given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string.
4+
5+
You can swap the characters at any pair of indices in the given pairs any number of times.
6+
7+
Return the lexicographically smallest string that s can be changed to after using the swaps.
8+
9+
Example 1:
10+
11+
```text
12+
Input: s = "dcab", pairs = [[0,3],[1,2]]
13+
Output: "bacd"
14+
Explanation:
15+
Swap s[0] and s[3], s = "bcad"
16+
Swap s[1] and s[2], s = "bacd"
17+
```
18+
19+
Example 2:
20+
21+
```text
22+
Input: s = "dcab", pairs = [[0,3],[1,2],[0,2]]
23+
Output: "abcd"
24+
Explanation:
25+
Swap s[0] and s[3], s = "bcad"
26+
Swap s[0] and s[2], s = "acbd"
27+
Swap s[1] and s[2], s = "abcd"
28+
```
29+
30+
Example 3:
31+
32+
```text
33+
Input: s = "cba", pairs = [[0,1],[1,2]]
34+
Output: "abc"
35+
Explanation:
36+
Swap s[0] and s[1], s = "bca"
37+
Swap s[1] and s[2], s = "bac"
38+
Swap s[0] and s[1], s = "abc"
39+
```
40+
41+
Constraints:
42+
43+
- `1 <= s.length <= 10^5`
44+
- `0 <= pairs.length <= 10^5`
45+
- `0 <= pairs[i][0], pairs[i][1] < s.length`
46+
- `s only contains lower case English letters.`
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package problem1202
2+
3+
import "sort"
4+
5+
func smallestStringWithSwaps(s string, pairs [][]int) string {
6+
n := len(s)
7+
un := newUnion(n)
8+
9+
for _, p := range pairs {
10+
un.unite(p[0], p[1])
11+
}
12+
13+
group := make(map[int][]int, n)
14+
for c, p := range un.parent {
15+
group[un.find(p)] = append(group[un.find(p)], c)
16+
}
17+
18+
bytes := []byte(s)
19+
res := make([]byte, n)
20+
for _, children := range group {
21+
size := len(children)
22+
t := make([]int, size)
23+
copy(t, children)
24+
if size > 1 {
25+
sort.Slice(t, func(i, j int) bool {
26+
return bytes[t[i]] < bytes[t[j]]
27+
})
28+
sort.Ints(children)
29+
}
30+
for i := 0; i < size; i++ {
31+
res[children[i]] = bytes[t[i]]
32+
}
33+
}
34+
35+
return string(res)
36+
}
37+
38+
type union struct {
39+
parent []int
40+
}
41+
42+
func newUnion(size int) *union {
43+
parent := make([]int, size)
44+
for i := range parent {
45+
parent[i] = i
46+
}
47+
return &union{
48+
parent: parent,
49+
}
50+
}
51+
52+
func (u *union) unite(x, y int) {
53+
u.parent[u.find(x)] = u.find(y)
54+
}
55+
56+
func (u *union) find(i int) int {
57+
if u.parent[i] != i {
58+
u.parent[i] = u.find(u.parent[i])
59+
}
60+
return u.parent[i]
61+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package problem1202
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
s string
12+
pairs [][]int
13+
ans string
14+
}{
15+
16+
// {
17+
// "dcab",
18+
// [][]int{{0, 3}, {1, 2}},
19+
// "bacd",
20+
// },
21+
22+
{
23+
"dcab",
24+
[][]int{{0, 3}, {1, 2}, {0, 2}},
25+
"abcd",
26+
},
27+
28+
{
29+
"cba",
30+
[][]int{{0, 1}, {1, 2}},
31+
"abc",
32+
},
33+
34+
// 可以有多个 testcase
35+
}
36+
37+
func Test_smallestStringWithSwaps(t *testing.T) {
38+
a := assert.New(t)
39+
40+
for _, tc := range tcs {
41+
a.Equal(tc.ans, smallestStringWithSwaps(tc.s, tc.pairs), "输入:%v", tc)
42+
}
43+
}
44+
45+
func Benchmark_smallestStringWithSwaps(b *testing.B) {
46+
for i := 0; i < b.N; i++ {
47+
for _, tc := range tcs {
48+
smallestStringWithSwaps(tc.s, tc.pairs)
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)