#2839
Easy Algorithms Check if strings can be made equal with operations i
String
47.8% acceptance
Feb 25, 2026
200
30
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.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn can_be_equal(s1: String, s2: String) -> bool {
let b1 = s1.as_bytes();
let b2 = s2.as_bytes();
let mut even1 = [b1[0], b1[2]]; even1.sort();
let mut even2 = [b2[0], b2[2]]; even2.sort();
let mut odd1 = [b1[1], b1[3]]; odd1.sort();
let mut odd2 = [b2[1], b2[3]]; odd2.sort();
even1 == even2 && odd1 == odd2
}
}