1790. Check if One String Swap Can Make Strings Equal

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
bool areAlmostEqual(string s1, string s2) {
int n = s1.size(), count = 0;
vector<int> ret(26,0);
for(int i=0;i<n;++i){
if(s1[i]!=s2[i]) count++;
if(count>2) return false;
ret[s1[i]-'a']++;
ret[s2[i]-'a']--;
}
if(ret != vector<int>(26,0)) return false;
return true;
}
};

analysis

  • time complexity O(n)
  • space complexity O(1)