Codility-lesson7

Equeue·2021년 8월 29일

Codility

- lesson7

Multibrackets.py

def solution(S):
    lenS=len(S)
    hashmap={"}":"{","]":"[",")":"("}

    stack=[]

    for i in range(lenS):
        if S[i]=="{" or S[i]=="[" or S[i]=="(":
            stack.append(S[i])
        else:
            if len(stack)!=0 and stack[-1]==hashmap[S[i]]:
                stack.pop()
            else: # len(stack)==0 이거나 짝이 안맞게 ) } ] 들어올경우 
                return 0
    
    if len(stack)!=0: #stack 이 { [ ( 채워진경우  (짝이 안맞을 경우)
        return 0
    else:
        return 1
profile
Equeue's Develop Post

0개의 댓글