From 36f13cef14367773f7ec1a3f761456c9c37234fb Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Tue, 23 Nov 2021 10:44:27 +0800 Subject: [PATCH 1/3] add: leetcode 0859 solution --- .../0859.Buddy-Strings/859.Buddy Strings.go | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 leetcode/0859.Buddy-Strings/859.Buddy Strings.go diff --git a/leetcode/0859.Buddy-Strings/859.Buddy Strings.go b/leetcode/0859.Buddy-Strings/859.Buddy Strings.go new file mode 100644 index 00000000..8688f16e --- /dev/null +++ b/leetcode/0859.Buddy-Strings/859.Buddy Strings.go @@ -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] +} From 3d044414a290739b6191dd40a80e6ec9ef313e55 Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Tue, 23 Nov 2021 10:44:37 +0800 Subject: [PATCH 2/3] add: leetcode 0859 test --- .../859.Buddy Strings_test.go | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 leetcode/0859.Buddy-Strings/859.Buddy Strings_test.go diff --git a/leetcode/0859.Buddy-Strings/859.Buddy Strings_test.go b/leetcode/0859.Buddy-Strings/859.Buddy Strings_test.go new file mode 100644 index 00000000..3085e448 --- /dev/null +++ b/leetcode/0859.Buddy-Strings/859.Buddy Strings_test.go @@ -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") +} From 0a27b2bd288e7cadb714874ab59a633c7736740e Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Tue, 23 Nov 2021 10:44:49 +0800 Subject: [PATCH 3/3] add: leetcode 0859 readme --- leetcode/0859.Buddy-Strings/README.md | 86 +++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 leetcode/0859.Buddy-Strings/README.md diff --git a/leetcode/0859.Buddy-Strings/README.md b/leetcode/0859.Buddy-Strings/README.md new file mode 100644 index 00000000..0045a911 --- /dev/null +++ b/leetcode/0859.Buddy-Strings/README.md @@ -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] +} +``` \ No newline at end of file