#2840
Medium Algorithms Check if strings can be made equal with operations ii
Hash Table String Sorting
56.1% acceptance
Feb 25, 2026
272
5
You are given two strings s1 and s2, both of length n, 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 i < j and the difference j - i is even, 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 check_strings(s1: String, s2: String) -> bool {
let b1 = s1.as_bytes();
let b2 = s2.as_bytes();
let mut even1: Vec<u8> = b1.iter().step_by(2).cloned().collect();
let mut even2: Vec<u8> = b2.iter().step_by(2).cloned().collect();
let mut odd1: Vec<u8> = b1.iter().skip(1).step_by(2).cloned().collect();
let mut odd2: Vec<u8> = b2.iter().skip(1).step_by(2).cloned().collect();
even1.sort(); even2.sort(); odd1.sort(); odd2.sort();
even1 == even2 && odd1 == odd2
}
}