Skip to content

Commit 7423d5f

Browse files
committed
feat(859): finish buddy string
1 parent a422fd2 commit 7423d5f

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

kit/859.BuddyStrings/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [859. Buddy Strings](https://leetcode.com/problems/buddy-strings/)
2+
3+
## 2019/05/09
4+
5+
### 题目 💗[easy]
6+
7+
A 和 B 两个变量, A 你能够在对调两个字符串后 等于 B 则返回 true ,否则 flase

kit/859.BuddyStrings/main.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package buddyStrings
2+
3+
func buddyStrings(A string, B string) bool {
4+
if A == B {
5+
for i := range A {
6+
if i > 0 && A[i] == A[0] {
7+
return true
8+
}
9+
}
10+
return false
11+
} else {
12+
l, r := 0, len(A)-1
13+
for l < r {
14+
if A[r] != B[r] && A[l] != B[l] {
15+
runeA := []rune(A)
16+
runeA[r], runeA[l] = runeA[l], runeA[r]
17+
A = string(runeA)
18+
break
19+
} else {
20+
if A[l] == B[l] {
21+
l++
22+
}
23+
if A[r] == B[r] {
24+
r--
25+
}
26+
}
27+
}
28+
}
29+
return A == B
30+
}

0 commit comments

Comments
 (0)