Problem
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.
Algorithm
Determine whether two length-4 strings can be made equal by repeatedly swapping characters at indices differing by 2 (i.e., positions 0↔2 and 1↔3).
Code
class Solution:
def canBeEqual(self, s1: str, s2: str) -> bool:
return ((s1[0] == s2[0] and s1[2] == s2[2]) or \
(s1[0] == s2[2] and s1[2] == s2[0])) and \
((s1[1] == s2[1] and s1[3] == s2[3]) or \
(s1[1] == s2[3] and s1[3] == s2[1]))


6012

被折叠的 条评论
为什么被折叠?



