Easy
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:
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:
Input: s1 = “abcd”, s2 = “cdab”
Output: true
Explanation: We can do the following operations on s1:
Example 2:
Input: s1 = “abcd”, s2 = “dacb”
Output: false
Explanation: It is not possible to make the two strings equal.
Constraints:
s1.length == s2.length == 4
s1
and s2
consist only of lowercase English letters.public class Solution {
public boolean canBeEqual(String s1, String s2) {
return isOk(s1, s2, 0) && isOk(s1, s2, 1);
}
private boolean isOk(String s1, String s2, int i) {
char a = s1.charAt(i);
char b = s1.charAt(i + 2);
char c = s2.charAt(i);
char d = s2.charAt(i + 2);
if (a == c && b == d) {
return true;
}
return a == d && b == c;
}
}