✔️ Easiest Solution ✔️ | 🔥 HashMap + Brute Force 🔥 | 2 Solutions
1. Brute Force (Nested Loops)IntuitionWe check all possible swaps in s2. For every pair of characters, swap them and check if s1 == s2. If any swap makes them e
---
# 1. Brute Force (Nested Loops)
---

# Intuition
We check all possible swaps in `s2`. For every **pair of characters**, swap them and check if `s1 == s2`. If any swap makes them **equal**, return `true`. If no **swap works**, return `false`.


# Approach
`Step 1` **If s1 == s2, return true (they are already equal).**

`Step 2` **Try swapping every possible pair of characters in s2.**

`Step 3` **After each swap, check if s1 == s2.**

`Step 4` **If any swap works, return true.**

`Step 5` **If no valid swap exists, return false.**

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

# Code
```Java []
class Solution
{
    public boolean areAlmostEqual(String s1, String s2)
    
        // Step 1: If strings are already equal, return true
        if (s1.equals(s2)) return true;

        // Step 2: Try swapping every possible pair of characters in s2
        for (int i = 0; i < s1.length(); i++)
        {
            for (int j = i + 1; j < s1.length(); j++) 
            {
                char[] arr = s2.toCharArray();

                // Step 3: Swap characters at positions i and j
                char temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;

                // Step 4: If after swapping, s1 == s2, return true
                if (s1.equals(new String(arr))) return true;
            }
        }

        // Step 5: If no swap works, return false
        return false;
    }
}
```
```C++ []
class Solution
{
public:
    bool areAlmostEqual(string s1, string s2) 
    {
        // Step 1: If strings are already equal, return true
        if (s1 == s2) return true;

        // Step 2: Try swapping every possible pair of characters in s2
        for (int i = 0; i < s1.length(); i++)
        {
            for (int j = i + 1; j < s1.length(); j++)
            {

                // Step 3: Swap characters at positions i and j
                swap(s2[i], s2[j]);

                // Step 4: If after swapping, s1 == s2, return true
                if (s1 == s2) return true;

                // Undo the swap (backtracking)
                swap(s2[i], s2[j]);
            }
        }

        // Step 5: If no swap works, return false
        return false;
    }
};
```
---
# 2. Optimal (HashMap)
---

# Intuition
Imagine you have two nearly **identical strings**. You are allowed to swap only **one pair** of characters to make them **equal**. First, count the character `frequencies` using a `HashMap` and track **mismatch positions**. If mismatches are more than 2, return `false`. Otherwise, check if swapping the **mismatched characters** makes them identical.

# Approach
`Step 1` **If s1 == s2, return true (they are already equal)**.

`Step 2` **Create two HashMaps to count character occurrences in s1 and s2.**

`Step 3` **Track mismatch positions where s1[i] != s2[i].**

`Step 4` **If more than 2 mismatches, return false (as only one swap is allowed).**

`Step 5` **If character frequencies in both HashMaps are different, return false.**

`Step 6` **If exactly 2 mismatches, check if swapping fixes the issue.**

# Complexity
- Time complexity : $$O(n)$$
- Space complexity : $$O(n)^2$$

# Code
```Java []
class Solution
{
    public boolean areAlmostEqual(String s1, String s2)
    {
        // Step 1: If strings are already equal, return true
        if (s1.equals(s2)) return true;

        // Step 2: Initialize HashMaps to store character frequencies
        HashMap<Character, Integer> map1 = new HashMap<>();
        HashMap<Character, Integer> map2 = new HashMap<>();
        ArrayList<Integer> misMatch = new ArrayList<>();

        // Step 3: Traverse both strings and count character frequencies
        for (int i = 0; i < s1.length(); i++)
        {
            char c1 = s1.charAt(i), c2 = s2.charAt(i);

            map1.put(c1, map1.getOrDefault(c1, 0) + 1);
            map2.put(c2, map2.getOrDefault(c2, 0) + 1);

            // If characters at this position are different, record the mismatch
            if (c1 != c2) misMatch.add(i);

            // Step 4: If more than 2 mismatches, return false
            if (misMatch.size() > 2) return false;
        }

        // Step 5: If character frequencies are different, return false
        if (!map1.equals(map2)) return false;

        // Step 6: Check if swapping the mismatched characters fixes the issue
        return misMatch.size() == 2 &&
               s1.charAt(misMatch.get(0)) == s2.charAt(misMatch.get(1)) &&
               s1.charAt(misMatch.get(1)) == s2.charAt(misMatch.get(0));
    }
}

```
```C++ []
class Solution
{
public:
    bool areAlmostEqual(string s1, string s2)
    {
        // Step 1: If strings are already equal, return true
        if (s1 == s2) return true;

        // Step 2: Create HashMaps to count character frequencies
        unordered_map<char, int> map1, map2;
        vector<int> misMatch;

        // Step 3: Traverse both strings and track mismatches
        for (int i = 0; i < s1.length(); i++)
        {
            char c1 = s1[i], c2 = s2[i];

            map1[c1]++;
            map2[c2]++;

            // If characters at this position are different, store the index
            if (c1 != c2) misMatch.push_back(i);

            // Step 4: If more than 2 mismatches, return false
            if (misMatch.size() > 2) return false;
        }

        // Step 5: If character frequencies are different, return false
        if (map1 != map2) return false;

        // Step 6: Check if swapping the mismatched characters fixes the issue
        return misMatch.size() == 2 &&
               s1[misMatch[0]] == s2[misMatch[1]] &&
               s1[misMatch[1]] == s2[misMatch[0]];
    }
};
```
['Array', 'Hash Table', 'Two Pointers', 'String', 'Greedy', 'Ordered Map', 'String Matching', 'Counting', 'C++', 'Java']