#1790

Check if One String Swap Can Make Strings Equal

pupil · 445 · lc easy +26 · verified · 49.5% accepted · 1,707 likes · top 36%

Description

Given two strings s1 and s2 of equal length, a string swap picks two positions in one string and swaps their characters. Return true if at most one such swap on exactly one string can make s1 equal s2.

Example 1:

Input: s1 = "bank", s2 = "kanb"
Output: true
Explanation: For example, swap the first character with the last character of s2 to make "bank".

Example 2:

Input: s1 = "attack", s2 = "defend"
Output: false
Explanation: It is impossible to make them equal with one string swap.

Example 3:

Input: s1 = "kelb", s2 = "kelb"
Output: true
Explanation: The two strings are already equal, so no string swap operation is required.

Code

1
2
3