본문 바로가기
Problem Solving/Codility

[Python] 7.3. Nesting

by 부르르 2021. 3. 30.

app.codility.com/demo/results/training6WQ7HN-NGN/

 

Test results - Codility

A string S consisting of N characters is called properly nested if: S is empty; S has the form "(U)" where U is a properly nested string; S has the form "VW" where V and W are properly nested strings. For example, string "(()(())())" is properly nested but

app.codility.com


Task Score

100%

Correctness

100%

Performance

100%

Detected time complexity

O(N)

def solution(S):
    
    nest = {"(":")"}
    stack = []

    for s in S:
        if s in nest.keys():
            stack.append(s)
        elif s in nest.values():
            if stack:
                stack.pop()
            else:
                return 0        
    
    if not stack:
        return 1
    else:
        return 0

 

문제를 푸는데 도움이 된 테스트 케이스

')('
''
')'
'('
'vv'
'v(v)v'

 

728x90
반응형

'Problem Solving > Codility' 카테고리의 다른 글

[Python] DisappearingPairs  (0) 2021.07.15
[Python] 8.1. Dominator  (0) 2021.04.05
[Python] 7.2. Fish  (0) 2021.03.29
[Python] 7.1. Brackets  (0) 2021.03.27
[Python] 6.4. Triangle  (0) 2021.03.26

댓글