Leetcode Problem 1790. Check if One String Swap Can Make Strings Equal

1790. Check if One String Swap Can Make Strings Equal

Leetcode Solutions

Check for One Swap Equality

  1. If s1 is equal to s2, return true as no swap is needed.
  2. Initialize an empty list diff to store indices where s1 and s2 differ.
  3. Iterate through the characters of s1 and s2: a. If the characters at the current index are different, add the index to diff.
  4. If the length of diff is not 2, return false as we can't make the strings equal with one swap.
  5. Check if swapping the characters at the two indices in diff would make the strings equal: a. If s1[diff[0]] == s2[diff[1]] and s1[diff[1]] == s2[diff[0]], return true. b. Otherwise, return false.
UML Thumbnail

Character Count Comparison

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...