Beats 100% | Solution for LeetCode#1790
IntuitionWhen comparing two strings, we need to determine if we can make them equal by swapping at most one pair of characters. If the strings are already ident
![image.png](https://assets.leetcode.com/users/images/d471e48a-3c7f-46aa-9487-c74c708992ec_1738714772.752795.png)

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
When comparing two strings, we need to determine if we can make them equal by swapping at most one pair of characters. If the strings are already identical, we can return true immediately. Otherwise, we need to check if exactly two characters can be swapped to make the strings equal.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Initialize Variables:
	- Use variables i and j to store the indices of characters that differ between the two strings. Initialize them to -1.
	- Use a counter cnt to count the number of differing positions.
2. Iterate Through the Strings:
	- Traverse each character of the strings using a loop.
	- If characters at the current position differ, increment the counter cnt.
	- Store the index of the differing characters in i and j when cnt is 1 and 2 respectively.
3. Check Conditions:
	- If cnt is 0, the strings are already equal, so return true.
	- If cnt is 2, check if swapping the characters at positions i and j in one string makes it equal to the other string.
	- If both conditions are met, return true; otherwise, return false.

# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        int i=-1, j=-1;
        int cnt=0;

        for(int k=0; k<s1.length(); k++){
            if(s1.charAt(k)!=s2.charAt(k)){
                cnt++;
                if(i==-1) i=k;
                else if(j==-1) j=k;
            }
        }

        if(cnt==0) return true;
        else if(cnt==2 && s1.charAt(i)==s2.charAt(j) && s1.charAt(j)==s2.charAt(i)){
            return true;
        }


        return false;
    }
}
```
```python []
class Solution(object):
    def areAlmostEqual(self, s1, s2):
        """
        :type s1: str
        :type s2: str
        :rtype: bool
        """
        i=-1
        j=-1
        cnt=0
        for k in range(0, len(s1)):
            if s1[k]!=s2[k]:
                cnt+=1
                if i==-1: i=k
                elif j==-1: j=k
        
        if cnt==0: return True
        elif cnt==2 and s1[i]==s2[j] and s1[j]==s2[i]: return True

        return False
```
```cpp []
class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        int i = -1, j = -1;
        int cnt = 0;

        for (int k = 0; k < s1.length(); k++) {
            if (s1[k] != s2[k]) {
                cnt++;
                if (i == -1) {
                    i = k;
                } else if (j == -1) {
                    j = k;
                }
            }
        }

        if (cnt == 0) {
            return true;
        } else if (cnt == 2 && s1[i] == s2[j] && s1[j] == s2[i]) {
            return true;
        }

        return false;
    }
};

```


['Python', 'C++', 'Java', 'Python3']