Ctrl K

One Swap Away

Easy
You are given two strings of equal length, first and second. A **swap** picks two positions in a string and exchanges the letters at them. Return True if the strings can be made identical with at most one swap in just one of them. Otherwise, return False.
Example 1
first="lemon", second="lomen" → True

Swapping e and o in the first string is enough.

Example 2
first="stone", second="notes" → False

The strings differ in four positions, which one swap cannot fix.

Example 3
first="pear", second="pear" → True

The strings already match, so no swap is needed.

Constraints
  • 1 ≤ len(first) = len(second) ≤ 10⁵
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNote From Clipped Letters EasyFirst One-Off Letter EasyWords Heard Only Once EasyWords From Letter Tiles EasyBalloon Signs From Letters Easy
loading Python…
Input
first="lemon", second="lomen"
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 106 tests on the server, hidden ones included.