본문 바로가기

Codility

[Lesson 7][Stacks and Queues] Brackets

app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/

 

Brackets coding task - Learn to Code - Codility

Determine whether a given string of parentheses (multiple types) is properly nested.

app.codility.com

# 괄호 짝이 맞는지

def solution(S):
    if not S:
        return 1

    st = []
    for ch in S:
        if ch in '{[(':
            st.append(ch)
        elif ch == ')':
            if st and st[-1] == '(':
                st.pop()
            else:
                return 0
        elif ch == ']':
            if st and st[-1] == '[':
                st.pop()
            else:
                return 0
        elif ch == '}':
            if st and st[-1] == '{':
                st.pop()
            else:
                return 0
    if st:
        return 0
    return 1


print(solution("{[()()]}"))  # 1

'Codility' 카테고리의 다른 글