🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏| JAVA | C | C++ | PYTHON| JAVASCRIPT | DART 
IntuitionThe problem requires checking whether two given strings, s1 and s2, can be made equal by swapping exactly one pair of characters.
If s1 and s2 are alre
![image.png](https://assets.leetcode.com/users/images/59280cb7-04cd-4d4d-aec8-e5da288861fe_1738738807.680259.png)


---

## **Intuition**
The problem requires checking whether two given strings, `s1` and `s2`, can be made equal by swapping exactly one pair of characters.  
- If `s1` and `s2` are already equal, return `true`.  
- If they differ at exactly two positions, we check if swapping the characters at those positions makes them equal.  
- If they differ at more than two positions, return `false`.  

---

## **Approach**
1. **Check if both strings are already equal** → Return `true`.  
2. **Find the mismatched positions** between `s1` and `s2`.  
3. **If mismatches exceed 2**, return `false`.  
4. **Validate swap possibility** → The two mismatched positions should have swappable characters.  
5. **Return true if swap makes them equal**, else return `false`.  

---

## **Complexity Analysis**
- **Time Complexity**: \(O(n)\), where \( n \) is the length of the strings. We iterate through the strings once.  
- **Space Complexity**: \(O(1)\), as we use only a few integer variables.

---


```dart []
class Solution {
  bool areAlmostEqual(String s1, String s2) {
    if (s1 == s2) return true;
    int firstMisMatch = -1, secondMisMatch = -1;
    int n = s1.length;

    for (int i = 0; i < n; i++) {
      if (s1[i] == s2[i]) continue;
      if (firstMisMatch == -1) {
        firstMisMatch = i;
      } else if (secondMisMatch == -1) {
        secondMisMatch = i;
      } else {
        return false;
      }
    }
    
    return secondMisMatch != -1 &&
        s1[firstMisMatch] == s2[secondMisMatch] &&
        s1[secondMisMatch] == s2[firstMisMatch];
  }
}
```


```java []
class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        if (s1.equals(s2)) return true;
        int firstMisMatch = -1, secondMisMatch = -1;
        int n = s1.length();

        for (int i = 0; i < n; i++) {
            if (s1.charAt(i) == s2.charAt(i)) continue;
            if (firstMisMatch == -1) firstMisMatch = i;
            else if (secondMisMatch == -1) secondMisMatch = i;
            else return false;
        }

        return secondMisMatch != -1 &&
                s1.charAt(firstMisMatch) == s2.charAt(secondMisMatch) &&
                s1.charAt(secondMisMatch) == s2.charAt(firstMisMatch);
    }
}
```

```javascript []
var areAlmostEqual = function(s1, s2) {
    if (s1 === s2) return true;
    let firstMisMatch = -1, secondMisMatch = -1;

    for (let i = 0; i < s1.length; i++) {
        if (s1[i] === s2[i]) continue;
        if (firstMisMatch === -1) firstMisMatch = i;
        else if (secondMisMatch === -1) secondMisMatch = i;
        else return false;
    }

    return secondMisMatch !== -1 && s1[firstMisMatch] === s2[secondMisMatch] &&
           s1[secondMisMatch] === s2[firstMisMatch];
};
```


```python []
def areAlmostEqual(s1: str, s2: str) -> bool:
    if s1 == s2:
        return True
    firstMisMatch = -1
    secondMisMatch = -1

    for i in range(len(s1)):
        if s1[i] == s2[i]:
            continue
        if firstMisMatch == -1:
            firstMisMatch = i
        elif secondMisMatch == -1:
            secondMisMatch = i
        else:
            return False

    return secondMisMatch != -1 and s1[firstMisMatch] == s2[secondMisMatch] and \
           s1[secondMisMatch] == s1[firstMisMatch]
```


```cpp []
class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        if (s1 == s2) return true;
        int firstMisMatch = -1, secondMisMatch = -1;

        for (int i = 0; i < s1.length(); i++) {
            if (s1[i] == s2[i]) continue;
            if (firstMisMatch == -1) firstMisMatch = i;
            else if (secondMisMatch == -1) secondMisMatch = i;
            else return false;
        }

        return secondMisMatch != -1 && s1[firstMisMatch] == s2[secondMisMatch] &&
               s1[secondMisMatch] == s1[firstMisMatch];
    }
};
```

---


![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style='width:250px'}

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