File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments