Skip to main content
Back to problems
#2716
Easy Algorithms

Minimize string length

Hash Table String
78.9% acceptance
Feb 25, 2026
375
109
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn minimized_string_length(s: String) -> i32 {
    s.chars().collect::<std::collections::HashSet<_>>().len() as i32
  }
}