LeetCode 3223, Medium. Topics: Hash Table, String, Counting. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 24 parametrized pytest cases, and a playground notebook:
Problem
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.
Examples
Constraints
- 1 <= s.length <= 2 * 10^5
- s consists only of lowercase English letters.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026