1. How I sloved
If two strings exchanged two characters, it was necessary to make sure that the two strings were equal. First, if the lengths of the two strings were different, no matter what, they could not be the same. And the index of the location where the strings were different was stored. If the two strings were the same, but each string contained the same character, it could have been the same string. It meant that if the number of the indexes were not 2, even swapping the text would not make them the same. Finally, when the two letters corresponding to the index I had previously discovered were exchanged, ‘True’ was returned if the two lines were the same. Otherwise ‘False’ was returned.
2. Code
class Solution:
def buddyStrings(self, A: str, B: str) -> bool:
if len(A) != len(B) or len(A)==1 or len(B)==1: return False
diff = []
for i in range(len(A)):
if A[i] != B[i]: diff.append(i)
if len(diff)==0:
for e in A:
temp = list(A[:])
temp.remove(e)
if e in temp: return True
if len(diff) != 2: return False
if A[diff[0]]==B[diff[1]] and A[diff[1]] == B[diff[0]]: return True
return False
3. Result
Runtime : 24 ms(96.73%), Memory usage : 14.1 MB(100.00%)
(Runtime can be different by a system even if it is a same code.)