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 a string s.
You can perform the following process on s any number of times:
i in the string such that there is at least one character to the left of index i that is equal to s[i], and at least one character to the right that is also equal to s[i].s[i] located to the left of i.s[i] located to the right of i.Return the minimum length of the final string s that you can achieve.
Example 1:
Input: s = "abaacbcbb"
Output: 5
Explanation:
We do the following operations:
s = "bacbcbb".s = "acbcb".Example 2:
Input: s = "aa"
Output: 2
Explanation:
We cannot perform any operations, so we return the length of the original string.
Constraints:
1 <= s.length <= 2 * 105s consists only of lowercase English letters.Problem summary: You are given a string s. You can perform the following process on s any number of times: Choose an index i in the string such that there is at least one character to the left of index i that is equal to s[i], and at least one character to the right that is also equal to s[i]. Delete the closest occurrence of s[i] located to the left of i. Delete the closest occurrence of s[i] located to the right of i. Return the minimum length of the final string s that you can achieve.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"abaacbcbb"
"aa"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3223: Minimum Length of String After Operations
class Solution {
public int minimumLength(String s) {
int[] cnt = new int[26];
for (int i = 0; i < s.length(); ++i) {
++cnt[s.charAt(i) - 'a'];
}
int ans = 0;
for (int x : cnt) {
if (x > 0) {
ans += x % 2 == 1 ? 1 : 2;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3223: Minimum Length of String After Operations
func minimumLength(s string) (ans int) {
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
for _, x := range cnt {
if x > 0 {
if x&1 == 1 {
ans += 1
} else {
ans += 2
}
}
}
return
}
# Accepted solution for LeetCode #3223: Minimum Length of String After Operations
class Solution:
def minimumLength(self, s: str) -> int:
cnt = Counter(s)
return sum(1 if x & 1 else 2 for x in cnt.values())
// Accepted solution for LeetCode #3223: Minimum Length of String After Operations
// 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 #3223: Minimum Length of String After Operations
// class Solution {
// public int minimumLength(String s) {
// int[] cnt = new int[26];
// for (int i = 0; i < s.length(); ++i) {
// ++cnt[s.charAt(i) - 'a'];
// }
// int ans = 0;
// for (int x : cnt) {
// if (x > 0) {
// ans += x % 2 == 1 ? 1 : 2;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3223: Minimum Length of String After Operations
function minimumLength(s: string): number {
const cnt = new Map<string, number>();
for (const c of s) {
cnt.set(c, (cnt.get(c) || 0) + 1);
}
let ans = 0;
for (const x of cnt.values()) {
ans += x & 1 ? 1 : 2;
}
return ans;
}
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.