LeetCode solutions

3455. Minimum Length of String After Operations

My accepted Python solution to LeetCode problem 3455, Minimum Length of String After Operations, running in 147ms.

  • Difficulty: Medium
  • Python
  • Runtime 147ms
  • Memory 19.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 147ms, memory 19.2MB, accepted 2025-12-31.

python
class Solution:
    def minimumLength(self, s: str) -> int:
        from collections import Counter
        
        # Count frequency of each character
        freq = Counter(s)
        
        # For each character, we can delete pairs of same characters
        # on both sides of any occurrence
        # The minimum remaining is 1 if odd count, 2 if even count >= 2
        result = 0
        for count in freq.values():
            if count % 2 == 1:
                result += 1
            else:
                result += 2
        
        return result

Source