You are given two strings s1 and s2 of equal length. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at those indices.
Return True if it is possible to make both strings equal by performing at most one string swap on exactly one of the two strings. Otherwise, return False.
Function Signature
def are_almost_equal(s1: str, s2: str) -> bool:
...
Rules
-
The two chosen indices may be the same, which leaves the string unchanged. Two strings that are already equal therefore return
True
.
-
The swap, if any, is applied to only one of the two strings.
Constraints
-
1 <= len(s1), len(s2) <= 100
-
len(s1) == len(s2)
-
s1
and
s2
consist of only lowercase English letters.
Examples
Input: s1 = "bank", s2 = "kanb"
Output: True
The strings differ only at indices 0 and 3. Swapping the first and last characters of s2 gives "bank", which equals s1.
Input: s1 = "attack", s2 = "defend"
Output: False
The strings differ at all six indices. One swap changes at most two positions, so no single swap on either string can make them equal.
Input: s1 = "kelb", s2 = "kelb"
Output: True
The strings are already equal, so zero swaps are needed, which is within the limit of at most one.