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.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
Given a string s, you have two types of operation:
i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if exists).i in the string, and let c be the character in position i. Delete the closest occurrence of c to the right of i (if exists).Your task is to minimize the length of s by performing the above operations zero or more times.
Return an integer denoting the length of the minimized string.
Example 1:
Input: s = "aaabc"
Output: 3
Explanation:
i = 1 so c is 'a', then we remove s[2] as it is closest 'a' character to the right of s[1].s becomes "aabc" after this.i = 1 so c is 'a', then we remove s[0] as it is closest 'a' character to the left of s[1].s becomes "abc" after this.Example 2:
Input: s = "cbbd"
Output: 3
Explanation:
i = 2 so c is 'b', then we remove s[1] as it is closest 'b' character to the left of s[1].s becomes "cbd" after this.Example 3:
Input: s = "baadccab"
Output: 4
Explanation:
i = 6 so c is 'a', then we remove s[2] as it is closest 'a' character to the left of s[6].s becomes "badccab" after this.i = 0 so c is 'b', then we remove s[6] as it is closest 'b' character to the right of s[0].s becomes "badcca" fter this.i = 3 so c is 'c', then we remove s[4] as it is closest 'c' character to the right of s[3].s becomes "badca" after this.i = 4 so c is 'a', then we remove s[1] as it is closest 'a' character to the left of s[4].s becomes "bdca" after this.Constraints:
1 <= s.length <= 100s contains only lowercase English lettersProblem summary: Given a string s, you have two types of operation: Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if exists). Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the right of i (if exists). Your task is to minimize the length of s by performing the above operations zero or more times. Return an integer denoting the length of the minimized string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"aaabc"
"cbbd"
"baadccab"
remove-all-adjacent-duplicates-in-string)remove-all-adjacent-duplicates-in-string-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2716: Minimize String Length
class Solution {
public int minimizedStringLength(String s) {
Set<Character> ss = new HashSet<>();
for (int i = 0; i < s.length(); ++i) {
ss.add(s.charAt(i));
}
return ss.size();
}
}
// Accepted solution for LeetCode #2716: Minimize String Length
func minimizedStringLength(s string) int {
ss := map[rune]struct{}{}
for _, c := range s {
ss[c] = struct{}{}
}
return len(ss)
}
# Accepted solution for LeetCode #2716: Minimize String Length
class Solution:
def minimizedStringLength(self, s: str) -> int:
return len(set(s))
// Accepted solution for LeetCode #2716: Minimize String Length
use std::collections::HashSet;
impl Solution {
pub fn minimized_string_length(s: String) -> i32 {
let ss: HashSet<char> = s.chars().collect();
ss.len() as i32
}
}
// Accepted solution for LeetCode #2716: Minimize String Length
function minimizedStringLength(s: string): number {
return new Set(s.split('')).size;
}
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.