※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1728315071.A.0E2.html
就用stack掃過去
要檢查一下stk.empty()
不然會吃index out of range
def minLength(self, s: str) -> int:
stk = []
for c in s:
if c=='B' and len(stk)>0 and stk[-1]=='A':
stk.pop()
elif c=='D' and len(stk)>0 and stk[-1]=='C':
stk.pop()
else:
stk.append(c)
return len(stk)
--