Skip to main content
Back to problems
Leetcode
Medium
Strings
Hash Maps
Check If One String Swap Can Make Strings Equal

Determine whether two strings can be made equal by swapping exactly one pair of characters in the first string.

Acceptance 0%
Problem Statement

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, s1 and s2.
  • Both strings have the same length.

Output Format

  • Return true if the strings can be made equal with at most one swap in s1.
  • Otherwise, return false.

Constraints

  • 1 <= s1.length, s2.length <= 100
  • s1.length == s2.length
  • Strings contain lowercase English letters.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.