Best Solution for Arrays 🚀 in C++, Python and Java || 100% working
Intuition🧠 The problem asks us to determine if two strings can be made equal by swapping two characters in one string. If the strings are already equal, ✅ no sw

# Intuition
🧠 The problem asks us to determine if two strings can be made equal by swapping two characters in **one string**. If the strings are already equal, ✅ no swap is needed. Otherwise, we find positions where they differ and check if swapping can fix them.

# Approach
🔍 Compare the two strings character by character:
- Find positions where they differ.
- If more than 2 differences, ❌ return `false`.
- If exactly 2 differences, check if swapping makes them equal.

# Complexity
- Time complexity: $$O(n)$$, where $$n$$ is the string length.
- Space complexity: $$O(1)$$.

# Code
```cpp []
class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        if(s1==s2) return true;
        vector<int> arr;
        for(int i=0;i<s1.size();i++){
            if(s1[i]!=s2[i]) arr.push_back(i);
        }
        if(arr.size()!=2) return false;
        int i=arr[0];
        int j=arr[1];
        if(s1[i] != s2[j] || s1[j]!=s2[i])return false;
        return true;
    }
};
```

```python []
class Solution:
    def areAlmostEqual(self, s1: str, s2: str) -> bool:
        if s1 == s2:
            return True
        diff = [i for i in range(len(s1)) if s1[i] != s2[i]]
        if len(diff) != 2:
            return False
        i, j = diff
        return s1[i] == s2[j] and s1[j] == s2[i]
```

```java []
class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        if (s1.equals(s2)) return true;
        List<Integer> diff = new ArrayList<>();
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i)) diff.add(i);
        }
        if (diff.size() != 2) return false;
        int i = diff.get(0), j = diff.get(1);
        return s1.charAt(i) == s2.charAt(j) && s1.charAt(j) == s2.charAt(i);
    }
}
```
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">

# Connect with me on LinkedIn for more insights! 🌟 Link in bio
['Hash Table', 'String', 'Counting', 'Python', 'C++', 'Java', 'Python3']