Check if One String Swap Can Make Strings Equal - Problem
You have two strings of equal length, and your task is to determine if you can make them identical by performing at most one character swap on exactly one of the strings.
A string swap operation allows you to choose any two positions in a string (they can be the same position) and exchange the characters at those positions. The challenge is to figure out whether a single such operation can transform one string to match the other perfectly.
Goal: Return
Examples:
•
•
A string swap operation allows you to choose any two positions in a string (they can be the same position) and exchange the characters at those positions. The challenge is to figure out whether a single such operation can transform one string to match the other perfectly.
Goal: Return
true if it's possible to make both strings equal with at most one swap, otherwise return false.Examples:
•
s1 = "bank", s2 = "kanb" → true (swap positions 0 and 2 in s1)•
s1 = "attack", s2 = "defend" → false (too many differences) Input & Output
example_1.py — Basic Swap Case
$
Input:
s1 = "bank", s2 = "kanb"
›
Output:
true
💡 Note:
We can swap characters at positions 0 and 3 in s1: 'bank' → 'kank'. Wait, that's not right. Let me recalculate: swapping positions 0 and 3 gives us 'kank', not 'kanb'. Actually, we need to swap positions 1 and 2: 'bank' → 'bnak'. That's still not right. The correct answer is swapping positions 0 and 2: 'bank' → 'nankb'. I need to be more careful here.
example_2.py — Already Equal
$
Input:
s1 = "aa", s2 = "aa"
›
Output:
true
💡 Note:
The strings are already identical, so no swap is needed. This counts as 'at most one swap' since zero swaps is less than one.
example_3.py — Too Many Differences
$
Input:
s1 = "attack", s2 = "defend"
›
Output:
false
💡 Note:
These strings have differences at every position. Since one swap can fix at most 2 character positions, it's impossible to make these strings equal with just one swap.
Visualization
Tap to expand
Understanding the Visualization
1
Count Differences
Compare the strings character by character and count mismatches
2
Decision Point
Based on difference count: 0→true, 2→check if swappable, other→false
3
Swap Validation
For 2 differences, verify that swapping those positions fixes both mismatches
4
Final Answer
Return true only if strings are already equal or can be fixed with exactly one swap
Key Takeaway
🎯 Key Insight: Mathematical constraint - one character swap affects exactly two positions, so it can only fix strings that differ in exactly 0 or 2 positions (and those 2 differences must be complementary).
Time & Space Complexity
Time Complexity
O(n)
Single pass through both strings to find differences
✓ Linear Growth
Space Complexity
O(1)
Only storing at most 4 indices for the different positions
✓ Linear Space
Constraints
- 1 ≤ s1.length, s2.length ≤ 100
- s1.length == s2.length
- s1 and s2 consist of only lowercase English letters
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code