forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuddy.py
More file actions
45 lines (42 loc) · 1.18 KB
/
buddy.py
File metadata and controls
45 lines (42 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution(object):
def buddyStrings(self, A, B):
"""
:type A: str
:type B: str
:rtype: bool
"""
if A == B:
if len(set(A)) == 1:
return True
else:
import collections
c = collections.Counter(A)
count = 0
for k,v in c.items():
if v > 1:
count += 1
if count >= 2:
return True
else:
return False
elif A == "" or B == "":
return False
elif len(A) != len(B):
return False
else:
n = len(A)
A = list(A)
B = list(B)
first = None
second = None
for i in range(n):
if A[i] != B[i]:
if first == None:
first = i
else:
second = i
A[first],A[second] = A[second],A[first]
if A == B:
return True
else:
return False