This commit is contained in:
parent
d0df9b92ca
commit
c3e77d1121
1 changed files with 21 additions and 0 deletions
21
easy/check_if_one_string_swap_can_make_strings_equals.py
Normal file
21
easy/check_if_one_string_swap_can_make_strings_equals.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal
|
||||
|
||||
from typing import Optional
|
||||
|
||||
class Solution:
|
||||
def areAlmostEqual(self, s1: str, s2: str) -> bool:
|
||||
first: Optional[int] = None
|
||||
second: Optional[int] = None
|
||||
for i in range(len(s1)):
|
||||
if s1[i] == s2[i]:
|
||||
continue
|
||||
if first is not None:
|
||||
if second is not None:
|
||||
return False
|
||||
if s1[first] != s2[i] or s1[i] != s2[first]:
|
||||
return False
|
||||
second = i
|
||||
else:
|
||||
first = i
|
||||
return not ((first is None) ^ (second is None))
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue