✅✅ Beats 100% & 0 ms | JAVA 🔥 | Simplest Program | Optimal Approach | O(n) & O(1)
Approach
If the lengths of s1 and s2 are not equal, return false because swapping cannot make two unequal-length strings identical.
If s1 is already equal to s2

# Approach
- If the lengths of `s1` and `s2` are not equal, return false because swapping cannot make two unequal-length strings identical.
- If `s1` is already equal to `s2`, return true because no swaps are needed.
- Creates a list idxs to store the positions (indices) where `s1` and `s2` have different characters.
- Loops through both strings and compares each character.
  - If characters at index `i` in `s1` and `s2` are not the same, the index is added to idxs
- If the number of mismatched indices is not exactly 2, return `false`
- Stores the value of the indeces at `idx1` and `idx2`
- Checks if swapping `s1[idx1]` with `s1[idx2]` would make s1 equal to s2.
- If this condition holds, return `true`.



# Complexity
- Time complexity: **O(N)**, we iterate through the strings once (finding mismatches), where N is the string length.

- Space complexity: **O(1)**, Only stores at most 2 indices.

# PLEASE UPVOTE IF IT HELPS 🤝🤝
![Upvote Image Leetcode.jpeg](https://assets.leetcode.com/users/images/9db9e97e-aeb5-4c83-9380-a96e6a500a4c_1738731045.5392976.jpeg)



# Code
```java []
class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        if(s1.length() != s2.length()) return false;
        if(s1.equals(s2)) return true;

        ArrayList<Integer> idxs = new ArrayList<>();
        int size = s1.length();

        for(int i=0; i<size; i++){
            if(s1.charAt(i) != s2.charAt(i)) idxs.add(i);
        }

        if(idxs.size() != 2) return false;

        int idx1 = idxs.get(0);
        int idx2 = idxs.get(1);

        if(s1.charAt(idx1) == s2.charAt(idx2)  && s1.charAt(idx2) == s2.charAt(idx1)){
            return true;
        }
        return false;
    }
}
```
['Array', 'Two Pointers', 'String', 'Counting', 'Java']