Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions leetcode3/변지협/v3/20. Valid Parentheses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'''
1. 아이디어 :
스택을 이용한다. 여는 괄호나 스택 top과 짝이 맞지 않는 문자는 push하고,
스택 top과 짝이 맞는 닫는 괄호가 나오면 pop한다.
모든 문자를 처리한 뒤 스택이 비어 있으면 유효한 괄호 문자열, 아니면 유효하지 않다.

2. 시간복잡도 :
o(n)

3. 자료구조/알고리즘 :
스택

'''

class Solution:
def isValid(self, s: str) -> bool:
stack = []
for e in s:
if not stack:
stack.append(e)
continue

if stack[len(stack)-1] == '(' and e == ')':
stack.pop()
continue
elif stack[len(stack)-1] == '[' and e == ']':
stack.pop()
continue
elif stack[len(stack)-1] == '{' and e == '}':
stack.pop()
continue

stack.append(e)

return True if not stack else False
Loading