Check if One String Swap Can Make Strings Equal

Can a single character swap on one string make the two equal? Count mismatch positions.

Easy
Time O(N) Space O(1)
LeetCode
4 min read
strings

Problem#

Given two equal-length strings s1 and s2, return true iff they are already equal OR a single swap of two positions in one string makes them equal.

Examples#

  • s1 = "bank", s2 = "kanb"true (swap positions 0 and 3 in s2).
  • s1 = "attack", s2 = "defend"false.
  • s1 = "kelb", s2 = "kelb"true.

Constraints#

  • 1 <= s1.length <= 100, lowercase letters.

Hints#

Hint
Find all mismatch positions. The strings are swappable iff there are exactly 0 or exactly 2 mismatches with crossed equality.

Approach#

Scan both strings. Collect mismatch indices in a vector. Return true if mismatches.size() == 0, or if it equals 2 and s1[i] == s2[j] && s1[j] == s2[i] for the two indices.

Solution#

Check if One String Swap Can Make Strings Equal
class Solution {
public:
bool areAlmostEqual(string s1, string s2) {
if (s1.size() != s2.size()) return false;
int a = -1, b = -1;
for (int i = 0; i < (int)s1.size(); ++i) {
if (s1[i] != s2[i]) {
if (a == -1) a = i;
else if (b == -1) b = i;
else return false;
}
}
if (a == -1) return true; // strings equal
if (b == -1) return false; // exactly one mismatch
return s1[a] == s2[b] && s1[b] == s2[a];
}
};

Editorial#

A swap touches exactly two indices, so the only valid mismatch counts are 0 (no swap needed) or 2 (one swap fixes both). Anything else cannot be fixed by a single swap. Constant memory; one pass.

Complexity#

  • Time: O(N).
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.