Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbad". - Choose the indices i = 1, j = 3. The resulting string is s1 = "cdab" = s2.
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 == 4s1 and s2 consist only of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"abcd" "cdab"
"abcd" "dacb"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2839: Check if Strings Can be Made Equal With Operations I
class Solution {
public boolean canBeEqual(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 #2839: Check if Strings Can be Made Equal With Operations I
func canBeEqual(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 #2839: Check if Strings Can be Made Equal With Operations I
class Solution:
def canBeEqual(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 #2839: Check if Strings Can be Made Equal With Operations I
// 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 #2839: Check if Strings Can be Made Equal With Operations I
// class Solution {
// public boolean canBeEqual(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 #2839: Check if Strings Can be Made Equal With Operations I
function canBeEqual(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.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.