


9012번 괄호랑 비슷해서 여기서 코드 수정 함
[BOJ / 백준] 9012번 괄호 python / 파이썬 스택 (tistory.com)
스택을 사용해서 풀어줌
1) "(" 일 때
2) ")" 일 때
3) "[" 일 때
4) "]" 일 때
while True:
arr = list(map(str, input()))
if len(arr) == 1 and arr[0] == ".":
break
stack = []
result = 0
for j in arr:
if j == '(':
stack.append(j)
elif j == ')':
if stack != []:
a = stack.pop()
if a == '[':
result = 1
break
elif stack == []:
result = 1
break
elif j == '[':
stack.append(j)
elif j == ']':
if stack != []:
b = stack.pop()
if b == '(':
result = 1
break
elif stack == []:
result = 1
break
if result == 1:
print("no")
elif stack != []:
print("no")
elif result == 0 and stack == []:
print("yes")
'알고리즘 > BOJ' 카테고리의 다른 글
| [BOJ / 백준] 11659번 구간 합 구 하기 4 python / 파이썬 누적합 / 시간초과 탈출 (0) | 2022.03.23 |
|---|---|
| [BOJ / 백준] 2164번 카드2 python / 파이썬 (0) | 2022.03.21 |
| [BOJ / 백준] 9012번 괄호 python / 파이썬 스택 (0) | 2022.03.09 |
| [BOJ / 백준] 1918번 후위 표기식 python / 파이썬 후위 표기법 변환/ 파이썬 리스트 문자열 변환 (0) | 2022.02.27 |
| [BOJ / 백준] 2309번 일곱난쟁이 python / 파이썬 투포인터 사용 (0) | 2022.02.18 |
댓글