forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck if One String Swap Can Make Strings Equal.cpp
36 lines (33 loc) · 1.24 KB
/
Check if One String Swap Can Make Strings Equal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Runtime: 0 ms (Top 100.00%) | Memory: 6.3 MB (Top 62.31%)
/*
https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal/
Find the number of positions which are different.
Now the strings can be made equal only if there are 0
different positions or 2 different positions and there are
chars on those positions which when swaped will make the
strings equal.
TC: O(N)
SC: O(1), At most 2 values are put in the diff_pos vector
*/
class Solution {
public:
bool areAlmostEqual(string s1, string s2) {
// find the number of diff positions
vector<int> diff_pos;
for(int i = 0; i < s1.size(); i++) {
if(s1[i] != s2[i])
diff_pos.emplace_back(i);
// If there are more than 2 char positions that differ,
// the single swap op cannot anyway make the two strings equal
if(diff_pos.size() > 2)
return false;
}
// when the chars are exactly the same
if(diff_pos.empty())
return true;
// only one pair of diff positions, check if swapping makes them equal
if(diff_pos.size() == 2)
swap(s1[diff_pos[0]], s1[diff_pos[1]]);
return s1 == s2;
}
};