-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
34 lines (28 loc) · 840 Bytes
/
solver.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
#
# Time complexity: O(n) (where "n" is the length of any of the strings)
# Space complexity: O(1)
#
def buddy_strings(a, b):
# Check string length match
if len(a) != len(b):
return False
# Check if same string with duplicated characters
if a == b and len(a) > len(set(a)):
return True
# Calculate the diff between the strings
diff_a, diff_b = [], []
for i in range(len(a)):
if a[i] != b[i]:
diff_a.append(a[i])
diff_b.append(b[i])
if len(diff_a) > 2:
return False
# We expect exactly two diffs to make one swap
# So, if the number of diffs is different of two, more or
# less than one swap is needed and, thus, "false"
if len(diff_a) != 2:
return False
# Check if the swaps match
if diff_a[0] != diff_b[1] or diff_a[1] != diff_b[0]:
return False
return True