2999 Check If Strings Can Be Made Equal With Operations I
2999 Check If Strings Can Be Made Equal With Operations I
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:
1
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*.
Example 1:
1
2
3
4
5
6
7
**Input:** s1 = "abcd", s2 = "cdab"
**Output:** true
**Explanation:** We can do the following operations on s1:
- Choose the indices i = 0, j = 2. The resulting string is s1 = "cbad".
- Choose the indices i = 1, j = 3. The resulting string is s1 = "cdab" = s2.
Example 2:
1
2
3
4
5
**Input:** s1 = "abcd", s2 = "dacb"
**Output:** false
**Explanation:** It is not possible to make the two strings equal.
Constraints:
1
2
s1.length == s2.length == 4
s1 and s2 consist only of lowercase English letters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def canBeEqual(self, s1: str, s2: str) -> bool:
def test(s1, s2, s):
#print(s1, s2, s)
if s1 == s2:
return True
if s1 in s:
return False
s.add(s1)
return test(s1[2] + s1[1] + s1[0] + s1[3], s2, s) or test(s1[0] + s1[3] + s1[2] + s1[1], s2, s)
return test(s1, s2, set())
This post is licensed under CC BY 4.0 by the author.