Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
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:
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.
Example 1:
Input: s1 = "abcdba", s2 = "cabdab" Output: true Explanation: We can apply the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbadba". - Choose the indices i = 2, j = 4. The resulting string is s1 = "cbbdaa". - Choose the indices i = 1, j = 5. The resulting string is s1 = "cabdab" = s2.
Example 2:
Input: s1 = "abe", s2 = "bea" Output: false Explanation: It is not possible to make the two strings equal.
Constraints:
n == s1.length == s2.length1 <= n <= 105s1 and s2 consist only of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"abcdba" "cabdab"
"abe" "bea"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2840: Check if Strings Can be Made Equal With Operations II
class Solution {
public boolean checkStrings(String s1, String s2) {
int[][] cnt = new int[2][26];
for (int i = 0; i < s1.length(); ++i) {
++cnt[i & 1][s1.charAt(i) - 'a'];
--cnt[i & 1][s2.charAt(i) - 'a'];
}
for (int i = 0; i < 26; ++i) {
if (cnt[0][i] != 0 || cnt[1][i] != 0) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #2840: Check if Strings Can be Made Equal With Operations II
func checkStrings(s1 string, s2 string) bool {
cnt := [2][26]int{}
for i := 0; i < len(s1); i++ {
cnt[i&1][s1[i]-'a']++
cnt[i&1][s2[i]-'a']--
}
for i := 0; i < 26; i++ {
if cnt[0][i] != 0 || cnt[1][i] != 0 {
return false
}
}
return true
}
# Accepted solution for LeetCode #2840: Check if Strings Can be Made Equal With Operations II
class Solution:
def checkStrings(self, s1: str, s2: str) -> bool:
return sorted(s1[::2]) == sorted(s2[::2]) and sorted(s1[1::2]) == sorted(
s2[1::2]
)
// Accepted solution for LeetCode #2840: Check if Strings Can be Made Equal With Operations II
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2840: Check if Strings Can be Made Equal With Operations II
// class Solution {
// public boolean checkStrings(String s1, String s2) {
// int[][] cnt = new int[2][26];
// for (int i = 0; i < s1.length(); ++i) {
// ++cnt[i & 1][s1.charAt(i) - 'a'];
// --cnt[i & 1][s2.charAt(i) - 'a'];
// }
// for (int i = 0; i < 26; ++i) {
// if (cnt[0][i] != 0 || cnt[1][i] != 0) {
// return false;
// }
// }
// return true;
// }
// }
// Accepted solution for LeetCode #2840: Check if Strings Can be Made Equal With Operations II
function checkStrings(s1: string, s2: string): boolean {
const cnt: number[][] = Array.from({ length: 2 }, () => Array.from({ length: 26 }, () => 0));
for (let i = 0; i < s1.length; ++i) {
++cnt[i & 1][s1.charCodeAt(i) - 97];
--cnt[i & 1][s2.charCodeAt(i) - 97];
}
for (let i = 0; i < 26; ++i) {
if (cnt[0][i] || cnt[1][i]) {
return false;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.