코테일지
알고리즘 with python.07
hatso
2021. 6. 13. 11:52
올바른 괄호
"("열린 괄호가 나온 횟수만큼 ")" 닫힌 괄호가 나와야 올바른 괄호입니다.
괄호의 계수가 같더라도 ")"닫힌 괄호가 먼저 나왔다면 틀린 괄호입니다.
코드
s = "(())()"
def is_correct_parenthesis(string):
stack = []
for i in range(len(string)):
if string[i] == "(":
stack.append(i) # 어떤 값이 들어가도 상관 x ( 여부 저장이기 때문이다.
elif string[i] == ")": # []
if len(stack) == 0:
return False
else:
stack.pop()
if len(stack) == 0:
return True
else:
return False
return
print(is_correct_parenthesis(s)) # True 를 반환해야 합니다!