3223. Minimum Length of String After Operations LeetCode Solution
In this guide, you will get 3223. Minimum Length of String After Operations LeetCode Solution with the best time and space complexity. The solution to Minimum Length of String After Operations problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Length of String After Operations solution in C++
- Minimum Length of String After Operations solution in Java
- Minimum Length of String After Operations solution in Python
- Additional Resources

Problem Statement of Minimum Length of String After Operations
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.
Example 1:
Input: s = “abaacbcbb”
Output: 5
Explanation:
We do the following operations:
Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = “bacbcbb”.
Choose index 3, then remove the characters at indices 0 and 5. The resulting string is 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 * 105
s consists only of lowercase English letters.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(26) = O(1)
3223. Minimum Length of String After Operations LeetCode Solution in C++
class Solution {
public:
int minimumLength(string s) {
int ans = 0;
vector<int> count(26);
for (const char c : s)
++count[c - 'a'];
for (int i = 0; i < 26; ++i)
if (count[i] > 0)
ans += count[i] % 2 == 0 ? 2 : 1;
return ans;
}
};
/* code provided by PROGIEZ */
3223. Minimum Length of String After Operations LeetCode Solution in Java
class Solution {
public int minimumLength(String s) {
int ans = 0;
int[] count = new int[26];
for (final char c : s.toCharArray())
++count[c - 'a'];
for (int i = 0; i < 26; ++i)
if (count[i] > 0)
ans += count[i] % 2 == 0 ? 2 : 1;
return ans;
}
}
// code provided by PROGIEZ
3223. Minimum Length of String After Operations LeetCode Solution in Python
class Solution:
def minimumLength(self, s: str) -> int:
count = collections.Counter(s)
return sum(2 if freq % 2 == 0 else 1 for freq in count.values())
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.