LeetCode solutions

2800. Minimum String Length After Removing Substrings

My accepted Python solution to LeetCode problem 2800, Minimum String Length After Removing Substrings, running in 3ms.

  • Difficulty: Easy
  • Python
  • Runtime 3ms
  • Memory 17.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3ms, memory 17.2MB, accepted 2026-01-01.

python
class Solution:
    def minLength(self, s: str) -> int:
        stack = []
        for c in s:
            if stack and ((stack[-1] == 'A' and c == 'B') or (stack[-1] == 'C' and c == 'D')):
                stack.pop()
            else:
                stack.append(c)
        return len(stack)

Source