2696.删除子串后的字符串最小长度
链接:2696.删除子串后的字符串最小长度
难度:Easy
标签:栈、字符串、模拟
简介:返回可获得的最终字符串的 最小 可能长度。
题解 1 - python
- 编辑时间:2024-01-10
- 执行用时:48ms
- 内存消耗:17.09MB
- 编程语言: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 and stack[-1] == 'C' and c == 'D': stack.pop()
else: stack.append(c)
return len(stack)