def isValid(s):
stack = []
table = {
')' :'(',
'}' :'{',
']' :'[',
}
for char in s:
if char not in table:
stack.append(char)
elif not stack or table[char] != stack.pop():
return False
return len(stack) == 0
-> isValid('()[]')
True
-> isValid('()[]{}')
True
-> isValid('()]}')
False
import collections
def removeDuplicateLetters(s):
counter , seen, stack = collections.Counter(s), set(), []
for char in s:
counter[char] -= 1
if char in seen:
continue
# 뒤에 붙일 문자가 남아 있다면 스택에서 제거
while stack and char < stack[-1] and counter[stack[-1]] > 0 :
seen.remove(stack.pop())
stack.append(char)
seen.add(char)
return "".join(stack)
-> removeDuplicateLetters("cbabc")
"abc"
-> removeDuplicateLetters("cbacdcbc")
"acdb"