Determine whether two strings can be made equal by swapping exactly one pair of characters in the first string.
You are given two strings of equal length. In one move, you may choose two different positions in the first string and swap the characters at those positions.
Return whether it is possible to make the first string equal to the second string using at most one such swap.
A swap is optional: if the strings are already equal, the answer should still be true only when a valid swap operation can be considered under the problem's rules.
Input Format
- Two strings,
s1ands2. - Both strings have the same length.
Output Format
- Return
trueif the strings can be made equal with at most one swap ins1. - Otherwise, return
false.
Constraints
1 <= s1.length, s2.length <= 100s1.length == s2.length- Strings contain lowercase English letters.
Example 1
Input
s1 = "bank", s2 = "kanb"
Output
true
Explanation
Swapping the first and last characters of "bank" gives "kanb".
Example 2
Input
s1 = "attack", s2 = "defend"
Output
false
Explanation
The strings differ in more than two positions, so one swap cannot make them equal.
Show 1 more example
Example 3
Input
s1 = "kelb", s2 = "kelb"
Output
false
Explanation
The strings are already equal, but there is no useful swap that keeps them equal under the intended rule set for this problem.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.