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

In this guide, you will get 1790. Check if One String Swap Can Make Strings Equal LeetCode Solution with the best time and space complexity. The solution to Check if One String Swap Can Make Strings Equal problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if One String Swap Can Make Strings Equal solution in C++
  4. Check if One String Swap Can Make Strings Equal solution in Java
  5. Check if One String Swap Can Make Strings Equal solution in Python
  6. Additional Resources
1790. Check if One String Swap Can Make Strings Equal LeetCode Solution image

Problem Statement of Check if One String Swap Can Make Strings Equal

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”.

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.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1790. Check if One String Swap Can Make Strings Equal LeetCode Solution in C++

class Solution {
 public:
  // Similar to 859. Buddy Strings
  bool areAlmostEqual(string s1, string s2) {
    vector<int> diffIndices;
    for (int i = 0; i < s1.length(); ++i)
      if (s1[i] != s2[i])
        diffIndices.push_back(i);
    return diffIndices.empty() || (diffIndices.size() == 2 &&
                                   s1[diffIndices[0]] == s2[diffIndices[1]] &&
                                   s1[diffIndices[1]] == s2[diffIndices[0]]);
  }
};
/* code provided by PROGIEZ */

1790. Check if One String Swap Can Make Strings Equal LeetCode Solution in Java

class Solution {
  public boolean areAlmostEqual(String s1, String s2) {
    List<Integer> diffIndices = new ArrayList<>();

    for (int i = 0; i < s1.length(); ++i)
      if (s1.charAt(i) != s2.charAt(i))
        diffIndices.add(i);

    return diffIndices.isEmpty() ||
        (diffIndices.size() == 2 &&
         s1.charAt(diffIndices.get(0)) == s2.charAt(diffIndices.get(1)) &&
         s1.charAt(diffIndices.get(1)) == s2.charAt(diffIndices.get(0)));
  }
}
// code provided by PROGIEZ

1790. Check if One String Swap Can Make Strings Equal LeetCode Solution in Python

class Solution:
  # Similar to 859. Buddy Strings
  def areAlmostEqual(self, s1: str, s2: str) -> bool:
    diffIndices = [i for i, (a, b) in enumerate(zip(s1, s2))
                   if a != b]
    return not diffIndices or (len(diffIndices) == 2 and
                               s1[diffIndices[0]] == s2[diffIndices[1]] and
                               s1[diffIndices[1]] == s2[diffIndices[0]])
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.