-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathareAlmostEqual.py
More file actions
32 lines (25 loc) · 930 Bytes
/
areAlmostEqual.py
File metadata and controls
32 lines (25 loc) · 930 Bytes
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
class Solution:
def areAlmostEqual(self, s1: str, s2: str) -> bool:
# A and B are new a and b
# after we omit the same elements
S1 =[]
S2 =[]
# Take only the characters which are
# different in both the strings
# for every pair of indices
for i in range(len(s1)):
# If the current characters differ
if s1[i]!= s2[i]:
S1.append(s1[i])
S2.append(s2[i])
# The strings were already equal
if len(S1)== len(S2)== 0:
return True
# If the lengths of the
# strings are two
if len(S1)== len(S2)== 2:
# If swapping these characters
# can make the strings equal
if S1[0]== S1[1] and S1[0]== S1[1]:
return True
return False