blob: 89d2190e5a20a9cc928380fca77b155b2548bd64 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# 2696. Minimum String Length After Removing Substrings
"""
you are given a string 's' consisting only of uppercase english letters. you
can apply some operations to this string where in one operation you can
remove any occurence of one of the sbustrings 'AB' or 'CD' from 's'. return
the minimum possible length of the resulting string that you can obtain. note
that the string concatenates after removing the substring and could produce
new 'AB' or 'CD' substrings
"""
class Solution(object):
def minLength(self, s):
"""
:type s: str
:rtype: int
"""
stack = []
for c in s:
if not stack:
stack.append(c)
continue
if c == "B" and stack[-1] == "A":
stack.pop()
elif c == "D" and stack[-1] == "C":
stack.pop()
else:
stack.append(c)
return len(stack)
if __name__ == "__main__":
obj = Solution()
print(obj.minLength(s="ABFCACDB"))
print(obj.minLength(s="ACBBD"))
|