C++ Solution || Beats 100%
IntuitionIf two strings are already equal, return true. Otherwise, check if they can be made equal by swapping exactly one pair of characters.Approach
Count the
# Intuition
If two strings are already equal, return `true`. Otherwise, check if they can be made equal by swapping exactly one pair of characters.

# Approach
1. Count the number of positions where `s1[i]` differs from `s2[i]`.
2. If the count is not exactly 0 or 2, return `false`.
3. Use a frequency array to verify that `s1` and `s2` contain the same characters.
4. If the conditions are met, return `true`.

# Complexity
- **Time complexity:** $$O(n)$$
- **Space complexity:** $$O(1)$$

# Code
```cpp
class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        int n = s1.size();
        int cnt = 0;
        vector<int> charr(26,0);

        for (int i = 0; i < n; i++) {
            charr[s1[i]-'a']++;
            if (s1[i] != s2[i]) {
                cnt++;
            }
        }

        for (char& ch : s2) {
            if (charr[ch-'a']==0) {
                return false;
            }
            charr[ch-'a']--;
        }

        if (cnt == 0 || cnt == 2)
            return true;
        return false;
    }
};
````

![d3550b79-52c8-4704-846f-c00daecb5632_1737784461.667478.png](https://assets.leetcode.com/users/images/66b00c17-d1b8-4b7a-abbd-a5dcdbcb6e03_1738736304.9413958.png)


['Hash Table', 'String', 'Counting', 'C++']