기타/Codility

[Easy] Nesting

백곰곰 2022. 6. 10. 23:05
728x90
반응형

[문제]

주어진 문자열이 완성된 괄호인지 확인하는 문제

https://app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/

 

Nesting coding task - Learn to Code - Codility

Determine whether a given string of parentheses (single type) is properly nested.

app.codility.com

[코드]

stack을 활용

시간복잡도 : O(N)

def solution(S):
    # write your code in Python 3.6
    stack = []

    for s in S :
        if not stack :
            stack.append(s)
        elif stack[-1] == '(' and s == ')' :
            stack.pop()
        else :
            stack.append(s)
    if stack :
        return 0
    else :
        return 1
728x90

'기타 > Codility' 카테고리의 다른 글

[Easy] MaxSliceSum  (0) 2022.06.11
[Demo] MissingInteger  (0) 2022.06.11
[Easy] MinPerimeterRectangle  (0) 2022.06.10
[Easy] FrogRiverOne  (0) 2022.06.09
[Easy] CountDistinctSlices - 70%  (0) 2022.06.08