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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 3ms, memory 17.2MB, accepted 2026-01-01.
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)