Very Simple Beginner Friendly ( 2 approaches )C/Java/C++/Python3 Solution (2 pass O(n) and 1 pass)✅✅
IntuitionApproach1(Freq+count)
first pass check for frequency match
secound pass check how many chars are out of place
if the count of mismatch is more then 2 w
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach1(Freq+count)
<!-- Describe your approach to solving the problem. -->
1. **first pass check for frequency match**
2. secound pass **check how many chars are out of place**
3. if the count of mismatch is **more then 2** we return false as **one swap can only affect 2 chars at most**
# 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 count = 0;
        int[] freq1 = new int[26];
        int[] freq2 = new int[26];
        for (int i = 0; i < s1.length(); i++) {
            freq1[s1.charAt(i) - 'a']++;
            freq2[s2.charAt(i) - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (freq1[i] != freq2[i])
                return false;
        }
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i))
                count++;
        }
        return count <= 2;
    }
}
```
```C++ []
class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        int count = 0;
        int freq1[26] = {0}, freq2[26] = {0};
        for (int i = 0; i < s1.length(); i++) {
            freq1[s1[i] - 'a']++;
            freq2[s2[i] - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (freq1[i] != freq2[i])
                return false;
        }
        for (int i = 0; i < s1.length(); i++) {
            if (s1[i] != s2[i])
                count++;
        }
        return count <= 2;
    }
};
```
```python3 []
class Solution:
    def areAlmostEqual(self, s1: str, s2: str) -> bool:
        count = 0
        freq1, freq2 = [0] * 26, [0] * 26
        for i in range(len(s1)):
            freq1[ord(s1[i]) - ord('a')] += 1
            freq2[ord(s2[i]) - ord('a')] += 1
        if freq1 != freq2:
            return False
        for i in range(len(s1)):
            if s1[i] != s2[i]:
                count += 1
        return count <= 2
```
```C []
bool areAlmostEqual(char* s1, char* s2) {
    int count = 0;
    int freq1[26] = {0}, freq2[26] = {0};
    int len = strlen(s1);

    for (int i = 0; i < len; i++) {
        freq1[s1[i] - 'a']++;
        freq2[s2[i] - 'a']++;
    }
    for (int i = 0; i < 26; i++) {
        if (freq1[i] != freq2[i])
            return false;
    }
    for (int i = 0; i < len; i++) {
        if (s1[i] != s2[i])
            count++;
    }
    return count <= 2;
}
```
# Approach2(Only Count and index Match)
<!-- Describe your approach to solving the problem. -->
1. In case Count>2 we return false(one swap only affects two chars)
2. we find the first two positions of mismatch and check whether swapping then gives us the equal strings
# Example with same freq s1="aba" s2="aab"
1. Here count of mismatches is only 2 ( b and a at wrong positions)
2. first mismatch at idx=1 and second at idx=2
3. we check whether swapping them gives us the output (true here )

# Example with diff freq s1="acc" s2="abb"
1. Here count of mismatch is 2 (c and c at wrong positions)
2. first mismatch at idx=1 and second and idx=2 
3. we check whether swapping them gives us equal strings ( false here )
# 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 count = 0, n = s1.length(), idx1 = 0, idx2 = 0;
        for (int i = 0; i < n; i++) {
            if (s1.charAt(i) != s2.charAt(i)) {
                count++;
                if (count > 2) return false;
                
                if (count == 1) idx1 = i;
                else idx2 = i;
            }
        }
        return (s1.charAt(idx2) == s2.charAt(idx1))&&(s1.charAt(idx1) == s2.charAt(idx2));
    }
}
```
```C []
bool areAlmostEqual(char* s1, char* s2) {
    int count = 0, n = strlen(s1), idx1 = 0, idx2 = 0;
    for (int i = 0; i < n; i++) {
        if (s1[i] != s2[i]) {
            count++;
            if (count > 2) return false;
            if (count == 1) idx1 = i;
            else idx2 = i;
        }
    }
    return s1[idx2] == s2[idx1] && s1[idx1] == s2[idx2];
}
```
```C++ []
class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        int count = 0, n = s1.length(), idx1 = 0, idx2 = 0;
        for (int i = 0; i < n; i++) {
            if (s1[i] != s2[i]) {
                count++;
                if (count > 2) return false;
                if (count == 1) idx1 = i;
                else idx2 = i;
            }
        }
        return s1[idx2] == s2[idx1] && s1[idx1] == s2[idx2];
    }
};
```
```Python3 []
class Solution:
    def areAlmostEqual(self, s1: str, s2: str) -> bool:
        count, idx1, idx2 = 0, 0, 0
        for i in range(len(s1)):
            if s1[i] != s2[i]:
                count += 1
                if count > 2:
                    return False
                if count == 1:
                    idx1 = i
                else:
                    idx2 = i
        return s1[idx2] == s2[idx1] and s1[idx1] == s2[idx2]
```
['Hash Table', 'C', 'Counting', 'C++', 'Java', 'Python3']