Most Optimal Solution ✅Beats 100% | C++| Java | Python | JavaScript
⬆️Upvote if it helps ⬆️Connect with me on Linkedin [Bijoy Sing]🤔 IntuitionWe are given two strings, s1 and s2, and we need to determine if we can make them equa
# ⬆️Upvote if it helps ⬆️

---

## Connect with me on Linkedin [Bijoy Sing]

---

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

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

```java []
class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        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.isEmpty()) return true;
        if (diff.size() != 2) return false;
        
        return s1.charAt(diff.get(0)) == s2.charAt(diff.get(1)) &&
               s1.charAt(diff.get(1)) == s2.charAt(diff.get(0));
    }
}
```

```javascript []
class Solution {
    areAlmostEqual(s1, s2) {
        let diff = [];
        
        for (let i = 0; i < s1.length; i++) {
            if (s1[i] !== s2[i]) diff.push(i);
        }
        
        if (diff.length === 0) return true;
        if (diff.length !== 2) return false;
        
        return s1[diff[0]] === s2[diff[1]] && s1[diff[1]] === s2[diff[0]];
    }
}
```


### 🤔 **Intuition**
We are given two strings, `s1` and `s2`, and we need to determine if we can make them equal by swapping at most **one** pair of characters in **one** of the strings.  

- If the strings are already equal ➝ ✅ **Return true**
- If they have more than **two mismatched positions** ➝ ❌ **Return false**
- If exactly **two positions** are mismatched, check:
  - Can swapping these two characters fix the mismatch?  
  - If yes, return ✅ **true**, else return ❌ **false**

---

### 🛠 **Approach**
1. **Find Mismatches** 🔍  
   - Traverse both strings and note the indices where `s1[i] ≠ s2[i]`.
   - Store these indices in a list called `diff`.

2. **Check the Cases** 📌  
   - If `diff` is **empty** ➝ Strings are already equal ✅  
   - If `diff` contains **more than 2 indices** ➝ More than one swap is needed ❌  
   - If `diff` contains **exactly 2 indices**, check:
     - Can swapping these two mismatched characters make them equal? 🤔  

---

### ⏳ **Complexity Analysis**
- **Time Complexity:** \(O(n)\) 🔥 (We iterate through the string once)  
- **Space Complexity:** \(O(1)\) 🏆 (We store at most 2 indices)

---

### ✨ **Example Walkthrough**
#### Example 1:
```cpp
s1 = "bank"
s2 = "kanb"
```
📌 **Mismatched indices:** `[0, 3]`  
- `s1[0] = 'b'`, `s2[0] = 'k'`  
- `s1[3] = 'k'`, `s2[3] = 'b'`  
🔄 **Swapping** `'b'` and `'k'` in `s1` makes it `"kanb"`, which matches `s2`.  
✅ **Return true**  

#### Example 2:
```cpp
s1 = "abcd"
s2 = "abdc"
```
📌 **Mismatched indices:** `[2, 3]`  
- `s1[2] = 'c'`, `s2[2] = 'd'`  
- `s1[3] = 'd'`, `s2[3] = 'c'`  
🔄 **Swapping** `'c'` and `'d'` in `s1` makes it `"abdc"`, which matches `s2`.  
✅ **Return true**  

#### Example 3:
```cpp
s1 = "abcd"
s2 = "wxyz"
```
📌 **Mismatched indices:** `[0, 1, 2, 3]`  
🚨 **More than 2 mismatches!** Cannot fix with one swap.  
❌ **Return false**  

---
### *If you have any questions or need further clarification, feel free to drop a comment! 😊*

['String', 'Counting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']