Give you two strings of equal length s1 and s2 . The steps of a string exchange operation are as follows : Select two subscripts in a string ( It doesn't have to be different ), And exchange the characters corresponding to the two subscripts .
If you perform at most one string exchange on one of the strings, you can make the two strings equal , return true ; otherwise , return false .
Input :s1 = “bank”, s2 = “kanb”
Output :true
explain : for example , In exchange for s2 The first and last characters in can get “bank”
Input :s1 = “attack”, s2 = “defend”
Output :false
explain : A string exchange cannot make two strings equal
Input :s1 = “kelb”, s2 = “kelb”
Output :true
explain : The two strings are equal , So there's no need for string swapping
Input :s1 = “abcd”, s2 = “dcba”
Output :false
1 <= s1.length, s2.length <= 100
s1.length == s2.length
s1 and s2 It's only made up of lowercase letters
class Solution:
def areAlmostEqual(self, s1: str, s2: str) -> bool:
if s1 == s2:
return True
ans1 = 0
ans2 = []
ans3 = []
for i in range(len(s1)):
if s1[i] != s2[i]:
ans1 += 1
ans2.append(s1[i])
ans3.append(s2[i])
if ans1 == 3:
return False
if ans1 == 2:
if ans2[0] in ans3 and ans2[1] in ans3:
return True
return False