Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b885294

Browse files
committed
add 859 array
1 parent 5819903 commit b885294

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎LeetCodeSolutions/859.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#==================================================
2+
#==> Title: 859. 亲密字符串
3+
#==> Author: Zhang zhen
4+
#==> Email: hustmatnoble.gmail.com
5+
#==> GitHub: https://github.com/MatNoble
6+
#==> Date: 2/1/2021
7+
#==================================================
8+
9+
"""
10+
https://leetcode-cn.com/problems/buddy-strings/
11+
"""
12+
13+
class Solution:
14+
def buddyStrings(self, A: str, B: str) -> bool:
15+
n, m = len(A), len(B)
16+
if n != m: return False
17+
if A == B:
18+
C = set()
19+
for a in A:
20+
if a in C: return True
21+
C.add(a)
22+
return False
23+
else:
24+
C = []
25+
for i in range(n):
26+
if A[i] != B[i]:
27+
C.append([A[i], B[i]])
28+
if len(C) >=3: return False
29+
return len(C) == 2 and C[0] == C[-1][::-1]
30+
31+
mat = Solution()
32+
A = 'acb'
33+
B = 'abc'
34+
A = 'aa'
35+
B = 'aa'
36+
mat.buddyStrings(A, B)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /