[LeetCode] Check if Strings Can be Made Equal With Operations II

2840. Check if Strings Can be Made Equal With Operations II

You are given two strings s1 and s2, both of length n, consisting of lowercase English letters.

You can apply the following operation on any of the two strings any number of times:

  • Choose any two indices i and j such that i < j and the difference j - i is even, then swap the two characters at those indices in the string.

Return true if you can make the strings s1 and s2 equal, and false otherwise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
bool checkStrings(string s1, string s2) {
string a = "", b = "", c = "", d = "";
for(int i = 0; i < s1.length(); i++) {
if(i & 1) b.push_back(s1[i]);
else a.push_back(s1[i]);
}
for(int i = 0; i < s2.length(); i++) {
if(i & 1) d.push_back(s2[i]);
else c.push_back(s2[i]);
}
sort(begin(a), end(a));
sort(begin(b), end(b));
sort(begin(c), end(c));
sort(begin(d), end(d));
return a == c and b == d;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/09/02/PS/LeetCode/check-if-strings-can-be-made-equal-with-operations-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.