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

2839. Check if Strings Can be Made Equal With Operations I

You are given two strings s1 and s2, both of length 4, 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 j - i = 2, 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
class Solution {
public:
bool canBeEqual(string s1, string s2) {
if(s1[0] != s2[0]) swap(s1[0], s1[2]);
if(s1[1] != s2[1]) swap(s1[1], s1[3]);
return s1 == s2;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/09/02/PS/LeetCode/check-if-strings-can-be-made-equal-with-operations-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.