Skip to content

Commit 31d140b

Browse files
authored
Merge pull request #1113 from 0xff-dev/1790
Add solution and test-cases for problem 1790
2 parents f642471 + 599b6da commit 31d140b

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [1790.Check if One String Swap Can Make Strings Equal][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two strings `s1` and `s2` of equal length. A **string swap** is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.
5+
6+
Return true if it is possible to make both strings equal by performing **at most one string swap** on **exactly one** of the strings. Otherwise, return `false`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: s1 = "bank", s2 = "kanb"
12+
Output: true
13+
Explanation: For example, swap the first character with the last character of s2 to make "bank".
1314
```
1415

15-
## 题意
16-
> ...
16+
**Example 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Check if One String Swap Can Make Strings Equal
23-
```go
18+
```
19+
Input: s1 = "attack", s2 = "defend"
20+
Output: false
21+
Explanation: It is impossible to make them equal with one string swap.
2422
```
2523

24+
**Example 3:**
25+
26+
```
27+
Input: s1 = "kelb", s2 = "kelb"
28+
Output: true
29+
Explanation: The two strings are already equal, so no string swap operation is required.
30+
```
2631

2732
## 结语
2833

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s1 string, s2 string) bool {
4+
if s1 == s2 {
5+
return true
6+
}
7+
8+
var a, b uint8
9+
diff := 0
10+
for i := range s1 {
11+
if s1[i] == s2[i] {
12+
continue
13+
}
14+
diff++
15+
if diff == 1 {
16+
a, b = s1[i], s2[i]
17+
} else if diff == 2 {
18+
// a, s1, b s2
19+
if !(a == s2[i] && b == s1[i]) {
20+
return false
21+
}
22+
} else {
23+
return false
24+
}
25+
}
26+
return diff != 1
527
}

leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/Solution_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
s1, s2 string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "bank", "kanb", true},
17+
{"TestCase2", "attack", "defend", false},
18+
{"TestCase3", "kelb", "kelb", true},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.s1, c.s2)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.s1, c.s2)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)