Advertisement

Check if One String Swap Can Make Strings Equal - LeetCode 1790 Solution

Check if One String Swap Can Make Strings Equal - Complete Solution Guide

Check if One String Swap Can Make Strings Equal is LeetCode problem 1790, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two strings s1 and s2 of equal length. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices. Return true if it is possible to make both strings equal by performing at most one string swap on exactly one of the strings. Otherwise, return false . 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". E

Detailed Explanation

The problem asks whether two strings of equal length can be made identical by performing at most one swap of characters within *one* of the strings. The swap can involve swapping two different characters or the same character with itself (in essence, doing nothing). The input consists of two strings, `s1` and `s2`, and the output is a boolean value: `true` if a single swap (or no swap) can make the strings equal, and `false` otherwise.

Solution Approach

The solution iterates through both strings simultaneously, comparing characters at corresponding positions. It maintains a counter for the number of differences and records the indices where differences occur. If the number of differing indices is 0 (strings are equal), or 2 (a single swap can potentially fix), it proceeds to check if a single swap is sufficient. If the number of differing indices is greater than 2, it's impossible to achieve equality with a single swap.

Step-by-Step Algorithm

  1. Step 1: Check if the strings are already equal. If so, return `true`.
  2. Step 2: Iterate through the strings, counting the number of differing character positions and storing the indices of these positions.
  3. Step 3: If the difference count is greater than 2, return `false`.
  4. Step 4: If the difference count is 2, check if swapping the characters at the stored indices makes the strings equal. If so, return `true`; otherwise, return `false`.
  5. Step 5: If the difference count is 0, return `true` (strings are already equal).

Key Insights

  • Insight 1: The problem only requires checking for at most one swap. This means we only need to consider the differences between the two strings. If there are more than two differences, it's impossible to make them equal with one swap.
  • Insight 2: Focusing on the indices where the characters differ is crucial. The solution efficiently tracks these differences without unnecessary iterations.
  • Insight 3: Handling the edge case where the strings are already equal is important for efficiency. The early return improves the overall time complexity.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, String, Counting.

Companies

Asked at: DoorDash.