mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
30
leetcode/0859.Buddy-Strings/859.Buddy Strings.go
Normal file
30
leetcode/0859.Buddy-Strings/859.Buddy Strings.go
Normal file
@ -0,0 +1,30 @@
|
||||
package leetcode
|
||||
|
||||
func buddyStrings(s string, goal string) bool {
|
||||
if len(s) != len(goal) || len(s) <= 1 {
|
||||
return false
|
||||
}
|
||||
mp := make(map[byte]int)
|
||||
if s == goal {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if _, ok := mp[s[i]]; ok {
|
||||
return true
|
||||
}
|
||||
mp[s[i]]++
|
||||
}
|
||||
return false
|
||||
}
|
||||
first, second := -1, -1
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] != goal[i] {
|
||||
if first == -1 {
|
||||
first = i
|
||||
} else if second == -1 {
|
||||
second = i
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return second != -1 && s[first] == goal[second] && s[second] == goal[first]
|
||||
}
|
56
leetcode/0859.Buddy-Strings/859.Buddy Strings_test.go
Normal file
56
leetcode/0859.Buddy-Strings/859.Buddy Strings_test.go
Normal file
@ -0,0 +1,56 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question859 struct {
|
||||
para859
|
||||
ans859
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
type para859 struct {
|
||||
s string
|
||||
goal string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
type ans859 struct {
|
||||
ans bool
|
||||
}
|
||||
|
||||
func Test_Problem859(t *testing.T) {
|
||||
|
||||
qs := []question859{
|
||||
|
||||
{
|
||||
para859{"ab", "ba"},
|
||||
ans859{true},
|
||||
},
|
||||
|
||||
{
|
||||
para859{"ab", "ab"},
|
||||
ans859{false},
|
||||
},
|
||||
|
||||
{
|
||||
para859{"aa", "aa"},
|
||||
ans859{true},
|
||||
},
|
||||
|
||||
{
|
||||
para859{"aaaaaaabc", "aaaaaaacb"},
|
||||
ans859{true},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 859------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans859, q.para859
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, buddyStrings(p.s, p.goal))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
86
leetcode/0859.Buddy-Strings/README.md
Normal file
86
leetcode/0859.Buddy-Strings/README.md
Normal file
@ -0,0 +1,86 @@
|
||||
# [859. Buddy Strings](https://leetcode-cn.com/problems/buddy-strings/)
|
||||
|
||||
## 题目
|
||||
|
||||
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.
|
||||
|
||||
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].
|
||||
|
||||
For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
|
||||
|
||||
**Example 1**:
|
||||
|
||||
Input: s = "ab", goal = "ba"
|
||||
Output: true
|
||||
Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.
|
||||
|
||||
**Example 2**:
|
||||
|
||||
Input: s = "ab", goal = "ab"
|
||||
Output: false
|
||||
Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.
|
||||
|
||||
**Example 3**:
|
||||
|
||||
Input: s = "aa", goal = "aa"
|
||||
Output: true
|
||||
Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.
|
||||
|
||||
**Example 4**:
|
||||
|
||||
Input: s = "aaaaaaabc", goal = "aaaaaaacb"
|
||||
Output: true
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= s.length, goal.length <= 2 * 10000
|
||||
- s and goal consist of lowercase letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你两个字符串 s 和 goal ,只要我们可以通过交换 s 中的两个字母得到与 goal 相等的结果,就返回 true;否则返回 false 。
|
||||
|
||||
交换字母的定义是:取两个下标 i 和 j (下标从 0 开始)且满足 i != j ,接着交换 s[i] 和 s[j] 处的字符。
|
||||
|
||||
例如,在 "abcd" 中交换下标 0 和下标 2 的元素可以生成 "cbad" 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
分为两种情况进行比较:
|
||||
- s等于goal,s中有重复元素就返回true,否则返回false
|
||||
- s不等于goal,s中有两个下标不同的字符与goal中对应下标的字符分别相等
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func buddyStrings(s string, goal string) bool {
|
||||
if len(s) != len(goal) || len(s) <= 1 {
|
||||
return false
|
||||
}
|
||||
mp := make(map[byte]int)
|
||||
if s == goal {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if _, ok := mp[s[i]]; ok {
|
||||
return true
|
||||
}
|
||||
mp[s[i]]++
|
||||
}
|
||||
return false
|
||||
}
|
||||
first, second := -1, -1
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] != goal[i] {
|
||||
if first == -1 {
|
||||
first = i
|
||||
} else if second == -1 {
|
||||
second = i
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return second != -1 && s[first] == goal[second] && s[second] == goal[first]
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user