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

Approach 01

class Solution {
  public:
    bool areAlmostEqual(string str1, string str2) {
        int length1 = str1.length();
        int length2 = str2.length();
        
        if (length1 != length2) {
            return false;
        }

        if (str1 == str2) {
            return true;
        }

        vector<char> mismatchStr1, mismatchStr2;
        
        for (int i = 0; i < length1; i++) {
            if (str1[i] != str2[i]) {
                mismatchStr1.push_back(str1[i]);
                mismatchStr2.push_back(str2[i]);
                if (mismatchStr1.size() > 2) {
                    return false;
                }
            }
        }

        if (mismatchStr1.size() != 2) {
            return false;
        }

        return (mismatchStr1[0] == mismatchStr2[1] && mismatchStr1[1] == mismatchStr2[0]);
    }
};
class Solution:
    def areAlmostEqual(self, str1: str, str2: str) -> bool:
        length1 = len(str1)
        length2 = len(str2)
        
        if length1 != length2:
            return False

        if str1 == str2:
            return True

        mismatchStr1 = []
        mismatchStr2 = []

        for i in range(length1):
            if str1[i] != str2[i]:
                mismatchStr1.append(str1[i])
                mismatchStr2.append(str2[i])
                if len(mismatchStr1) > 2:
                    return False

        if len(mismatchStr1) != 2:
            return False

        return mismatchStr1[0] == mismatchStr2[1] and mismatchStr1[1] == mismatchStr2[0]

Time Complexity:

  • String Comparison:

    Checking if str1 and str2 are equal takes \( O(N) \).

  • Mismatch Collection:

    Iterating through both strings to find mismatches takes \( O(N) \).

  • Final Check:

    Comparing at most two characters takes \( O(1) \).

  • Overall Time Complexity:

    \( O(N) \), where \( N \) is the length of the strings.

Space Complexity:

  • Auxiliary Storage:

    Two vectors store at most two characters each, contributing to \( O(1) \) space.

  • Other Variables:

    Integer variables (length1, length2) and loop counters contribute to \( O(1) \) space.

  • Overall Space Complexity:

    \( O(1) \).

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top