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

 

  • N개의 문자로 구성된 문자열 S는 다음과 같이 properly nested
    • S는 비어있다.
    • S는 "(U)" 형식을 갖고 있다. U는 properly nested
    • S는 V와 W가 적절하게 중첩된 문자열인 "VW" 형식을 갖는다.
  • 문자열 S가 적절하게 properly nested 되면 1 출력 아니면 0 출력
  • O(N)

 

def solution(S):
    stack = []
    for i in range(len(S)):
        input = S[i]
        if input == ')':
            if len(stack)==0:
                return 0
            if stack[-1]=='(':
                stack.pop()
        else:
            stack.append(input)
    if len(stack)==0:
        return 1
    else:
        return 0

 

+ Recent posts