Check if One String Swap Can Make Strings Equal

EasyStringshashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given two strings s1 and s2 of equal length. A *string swap* picks two indices in one string (they may be the same index) and exchanges the characters at those positions. Return true if you can make the strings equal by performing **at most one** swap on **exactly one** of the strings. Strings that are already equal return true.

Examples

Input: s1 = "bank", s2 = "kanb"
Output: true
Swap the first and last characters of s2 to get "bank".
Input: s1 = "attack", s2 = "defend"
Output: false
The strings differ in too many places.
Input: s1 = "kelb", s2 = "kelb"
Output: true
No swap is needed.

Constraints

  • 1 <= s1.length, s2.length <= 100
  • s1.length == s2.length
  • `s1` and `s2` consist of lowercase English letters.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems