-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0859_Buddy_Strings.py
37 lines (35 loc) · 1.03 KB
/
0859_Buddy_Strings.py
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
class Solution:
def buddyStrings(self, s: str, goal: str) -> bool:
if not len(s) == len(goal):
return False
s_1 = ""
g_1 = ""
s_2 = ""
g_2 = ""
diff_count = 0
dup_set = set()
isDup = False
for each_s, each_g in zip(s, goal):
if not each_s == each_g:
if diff_count == 0:
s_1 = each_s
g_1 = each_g
diff_count += 1
elif diff_count == 1:
s_2 = each_s
g_2 = each_g
diff_count += 1
else:
return False
else:
if not isDup:
if each_s in dup_set:
isDup = True
else:
dup_set.add(each_s)
if s_1 == g_2 and s_2 == g_1:
if diff_count == 0:
return isDup
return True
else:
return False