Skip to content

Commit

Permalink
Merge pull request #668 from 0xff-dev/1202
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1202
  • Loading branch information
6boris authored Oct 28, 2023
2 parents 7d939e6 + 1485956 commit e9042e4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 24 deletions.
41 changes: 28 additions & 13 deletions leetcode/1201-1300/1202.Smallest-String-With-Swaps/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
# [1202.Smallest String With Swaps][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
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.

You can swap the characters at any pair of indices in the given `pairs` **any number of times**.

Return the lexicographically smallest string that `s` can be changed to after using the swaps.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "dcab", pairs = [[0,3],[1,2]]
Output: "bacd"
Explaination:
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Smallest String With Swaps
```go
```
Input: s = "dcab", pairs = [[0,3],[1,2],[0,2]]
Output: "abcd"
Explaination:
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"
```

**Example 3:**

```
Input: s = "cba", pairs = [[0,1],[1,2]]
Output: "abc"
Explaination:
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
```

## 结语

Expand Down
59 changes: 58 additions & 1 deletion leetcode/1201-1300/1202.Smallest-String-With-Swaps/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
package Solution

func Solution(x bool) bool {
type unionFind1202 struct {
fatehr []int
}

func (u *unionFind1202) findFather(x int) int {
if u.fatehr[x] != x {
u.fatehr[x] = u.findFather(u.fatehr[x])
}
return u.fatehr[x]
}
func (u *unionFind1202) union(x, y int) {
fx := u.findFather(x)
fy := u.findFather(y)
if fx < fy {
u.fatehr[fy] = fx
} else {
u.fatehr[fx] = fy
}
}

type stroe1202 struct {
bytes [26]int
index int
}

func (s *stroe1202) selectByte() byte {
for s.bytes[s.index] == 0 {
s.index++
}
x := uint8(s.index) + 'a'
s.bytes[s.index]--
return x
}

func Solution(s string, pairs [][]int) string {
bs := []byte(s)
u := &unionFind1202{fatehr: make([]int, len(s))}
for i := 0; i < len(s); i++ {
u.fatehr[i] = i
}
for _, pair := range pairs {
u.union(pair[0], pair[1])
}

children := make(map[int]*stroe1202)
for idx := 0; idx < len(s); idx++ {
fidx := u.findFather(idx)
if _, ok := children[fidx]; !ok {
children[fidx] = &stroe1202{bytes: [26]int{}, index: 0}
}
children[fidx].bytes[bs[idx]-'a']++
}

for idx := 0; idx < len(bs); idx++ {
fidx := u.findFather(idx)
bs[idx] = children[fidx].selectByte()
}

return string(bs)
}
25 changes: 15 additions & 10 deletions leetcode/1201-1300/1202.Smallest-String-With-Swaps/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,35 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
pairs [][]int
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "dcab", [][]int{{0, 3}, {1, 2}}, "bacd"},
{"TestCase2", "dcab", [][]int{
{0, 3}, {1, 2}, {0, 2},
}, "abcd"},
{"TestCase3", "cba", [][]int{
{0, 1}, {1, 2},
}, "abc"},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.pairs)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.pairs)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit e9042e4

Please sign in to comment.