젊은이의 블로그

[자료구조와알고리즘with파이썬] Ch.01-3 스택의 응용: 괄호 검사 본문

책/자료구조와알고리즘with파이썬

[자료구조와알고리즘with파이썬] Ch.01-3 스택의 응용: 괄호 검사

젊은사람 등장 2024. 9. 24. 10:33
def checkBrackets(statement): //이름은 마음대로 설정
		stack = ArrayStack(100)
    	for ch in statement:
    		if ch in ('{','[','('):
        		stack.push(ch)
            elif ch in ('}',']',')'):
        		if stack.isEmpty():
            		return False
            	else:
            		left=stack.pop()
                	if(ch=="}" and left!="{") or\ (ch=="]" and left!= "[") or\ (ch==")" and left !="(":
                	return False
    	return stack.isEmpty()

 

if

elif (== else if)

else