Skip to content

Commit 21e84f1

Browse files
authored
Merge pull request halfrost#195 from gostool/leetcode0859
Leetcode0859
2 parents cb9ecdc + 0a27b2b commit 21e84f1

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
func buddyStrings(s string, goal string) bool {
4+
if len(s) != len(goal) || len(s) <= 1 {
5+
return false
6+
}
7+
mp := make(map[byte]int)
8+
if s == goal {
9+
for i := 0; i < len(s); i++ {
10+
if _, ok := mp[s[i]]; ok {
11+
return true
12+
}
13+
mp[s[i]]++
14+
}
15+
return false
16+
}
17+
first, second := -1, -1
18+
for i := 0; i < len(s); i++ {
19+
if s[i] != goal[i] {
20+
if first == -1 {
21+
first = i
22+
} else if second == -1 {
23+
second = i
24+
} else {
25+
return false
26+
}
27+
}
28+
}
29+
return second != -1 && s[first] == goal[second] && s[second] == goal[first]
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question859 struct {
9+
para859
10+
ans859
11+
}
12+
13+
// para 是参数
14+
type para859 struct {
15+
s string
16+
goal string
17+
}
18+
19+
// ans 是答案
20+
type ans859 struct {
21+
ans bool
22+
}
23+
24+
func Test_Problem859(t *testing.T) {
25+
26+
qs := []question859{
27+
28+
{
29+
para859{"ab", "ba"},
30+
ans859{true},
31+
},
32+
33+
{
34+
para859{"ab", "ab"},
35+
ans859{false},
36+
},
37+
38+
{
39+
para859{"aa", "aa"},
40+
ans859{true},
41+
},
42+
43+
{
44+
para859{"aaaaaaabc", "aaaaaaacb"},
45+
ans859{true},
46+
},
47+
}
48+
49+
fmt.Printf("------------------------Leetcode Problem 859------------------------\n")
50+
51+
for _, q := range qs {
52+
_, p := q.ans859, q.para859
53+
fmt.Printf("【input】:%v 【output】:%v\n", p, buddyStrings(p.s, p.goal))
54+
}
55+
fmt.Printf("\n\n\n")
56+
}

leetcode/0859.Buddy-Strings/README.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [859. Buddy Strings](https://leetcode-cn.com/problems/buddy-strings/)
2+
3+
## 题目
4+
5+
Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.
6+
7+
Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].
8+
9+
For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
10+
11+
**Example 1**:
12+
13+
Input: s = "ab", goal = "ba"
14+
Output: true
15+
Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.
16+
17+
**Example 2**:
18+
19+
Input: s = "ab", goal = "ab"
20+
Output: false
21+
Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.
22+
23+
**Example 3**:
24+
25+
Input: s = "aa", goal = "aa"
26+
Output: true
27+
Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.
28+
29+
**Example 4**:
30+
31+
Input: s = "aaaaaaabc", goal = "aaaaaaacb"
32+
Output: true
33+
34+
**Constraints:**
35+
36+
- 1 <= s.length, goal.length <= 2 * 10000
37+
- s and goal consist of lowercase letters.
38+
39+
## 题目大意
40+
41+
给你两个字符串 s 和 goal ,只要我们可以通过交换 s 中的两个字母得到与 goal 相等的结果,就返回 true;否则返回 false 。
42+
43+
交换字母的定义是:取两个下标 i 和 j (下标从 0 开始)且满足 i != j ,接着交换 s[i] 和 s[j] 处的字符。
44+
45+
例如,在 "abcd" 中交换下标 0 和下标 2 的元素可以生成 "cbad" 。
46+
47+
## 解题思路
48+
49+
分为两种情况进行比较:
50+
- s等于goal,s中有重复元素就返回true,否则返回false
51+
- s不等于goal,s中有两个下标不同的字符与goal中对应下标的字符分别相等
52+
53+
## 代码
54+
55+
```go
56+
package leetcode
57+
58+
func buddyStrings(s string, goal string) bool {
59+
if len(s) != len(goal) || len(s) <= 1 {
60+
return false
61+
}
62+
mp := make(map[byte]int)
63+
if s == goal {
64+
for i := 0; i < len(s); i++ {
65+
if _, ok := mp[s[i]]; ok {
66+
return true
67+
}
68+
mp[s[i]]++
69+
}
70+
return false
71+
}
72+
first, second := -1, -1
73+
for i := 0; i < len(s); i++ {
74+
if s[i] != goal[i] {
75+
if first == -1 {
76+
first = i
77+
} else if second == -1 {
78+
second = i
79+
} else {
80+
return false
81+
}
82+
}
83+
}
84+
return second != -1 && s[first] == goal[second] && s[second] == goal[first]
85+
}
86+
```

0 commit comments

Comments
 (0)