String 1 pass-> 1 line using zip||C++ Python beats 100%
IntuitionWhether it is possible to make both strings equal by performing at most one string swap on exactly one of the strings.1 pass for C++
Python code is 1 l
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Whether it is possible to make both strings equal by performing at most one string swap on exactly one of the strings.

1 pass for C++
Python code is 1 liner by using zip. C++ ranges also provides `views::zip` which has the similar function.
# Approach
<!-- Describe your approach to solving the problem. -->
1. let `n=|s1|`
2. let `cntD=0` denote the number of different characters
3. let `D={0, 0}` be the 2 elemented char array; 2 is enough to hold the different 2 characters ever seen.
4. Proceed the single loop 
```
// for i=0 to n-1 but when cntD2>2 break the loop
for(i=0; i<n && cntD<=2; i++){ 
    char x=s1[i], y=s2[i];
    if (x!=y){// different character
        cntD++; // increase cntD
        // if cntD>2 return false
        // or cntD==2  but they cannot be swapped also return false 
        if (cntD>2 ||(cntD==2 && (D[0]!=y || D[1]!=x )))
            return 0;
        D={x, y}; // D holds 2 characters x, y
    }
}

```
5. last check if `cntD==0 or cntD==2`.
6.  Python code is made; a 1-line code is presented; `zip` function is good.
7. C++20 `ranges::views::zip` is in use (Ref: https://en.cppreference.com/w/cpp/ranges/zip_view), 2nd C++ code follows the logic of the Python code using `zip`
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$O(n)$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$O(1)$
zip: $O(n)$
# Code||C++ 0ms beats 100%
```cpp []
class Solution {
public:
    bool areAlmostEqual(string& s1, string& s2) {
        const int n=s1.size();
        int cntD=0, i;
        array<char, 2> D={0};
        for(i=0; i<n && cntD<=2; i++){
            char x=s1[i], y=s2[i];
            if (x!=y){
                cntD++;
                if (cntD>2 ||(cntD==2 && (D[0]!=y || D[1]!=x )))
                    return 0;
                D={x, y};
            }
        }
        return cntD==0 || cntD==2;
    }
};
```
# zip is useful, that's good C++ ranges provides views::zip
Python zip is used to solve [867. Transpose Matrix](https://leetcode.com/problems/transpose-matrix/solutions/4383965/easy-transpose-c-c-python-zip-1-line/)
[Please turn on English subtitles]
[https://youtu.be/DvD1r0IOxPQ?si=c5_Qgu8pd59IcRJ4](https://youtu.be/DvD1r0IOxPQ?si=c5_Qgu8pd59IcRJ4)
#  Python 1-liner||0ms beats 100%
```
class Solution:
    def areAlmostEqual(self, s1: str, s2: str) -> bool:
        return len(D:=[(x, y) for x, y in zip(s1, s2) if x!=y])==0 or (len(D)==2 and D[0][0]==D[1][1] and D[0][1]==D[1][0])               
```
# Python 2 line version
```
class Solution:
    def areAlmostEqual(self, s1: str, s2: str) -> bool:
        D=[(x, y) for x, y in zip(s1, s2) if x!=y]
        return len(D)==0 or (len(D)==2 and D[0][0]==D[1][1] and D[0][1]==D[1][0])        
```
# 2nd C++ using ranges::views::zip ||0ms
```
#include <ranges>
class Solution {
public:
    bool areAlmostEqual(string& s1, string& s2) {
        vector<pair<char, char>> D;
        for (pair<char, char>&& xy : views::zip(s1, s2))
            if (xy.first!=xy.second) D.push_back(xy);
        return D.size()==0 ||( D.size()==2 && D[0].first==D[1].second && D[0].second==D[1].first);
    }
};
```

['String', 'C++', 'Python3']