Ctrl K

Exactly One Swap

Easy
You are given two strings, word and target. You must make exactly one swap: pick two *different* positions i and j in word and exchange the letters at them. The swap is required; you cannot skip it. Return True if word can become equal to target after such a swap, and False otherwise.
Example 1
word="stop", target="spot" → True

Swap t (position 1) and p (position 3): stop → spot.

Example 2
word="code", target="code" → False

The strings already match, but every letter is different, so any swap breaks the word. The answer is False.

Example 3
word="level", target="level" → True

You can swap the two ls (or the two es) and the word stays the same. The answer is True.

Constraints
  • 1 ≤ len(word), len(target) ≤ 2 · 10⁴
  • word and target consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasySame Letter Cipher EasySame Letters, Reshuffled EasySame When Flipped EasyShuffle Into a Palindrome Easy
loading Python…
Input
word="stop", target="spot"
Expected
True

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