본문 바로가기
Problem Solving/Codility

[Python] DisappearingPairs

by 부르르 2021. 7. 15.

https://app.codility.com/demo/results/trainingFRJG6A-A4D/

 

Test results - Codility

A string S containing only the letters "A", "B" and "C" is given. The string can be transformed by removing one occurrence of "AA", "BB" or "CC". Transformation of the string is the process of removing letters from it, based on the rules described above. A

app.codility.com


 

Point

  • 전형적인 stack과 관련된 알고리즘
  • 입력값과 stack의 가장 최근값 비교
  • 같다면 pop해서 stack에서 제거하고 다르다면 append하여 stack에 저장

Tip

  • stack 자료형 사용

Task Score

100%

Correctness

100%

Performance

100%

Detected time complexity

O(N)

def solution(S):
    
    stack = []

    for s in S:
        if stack and s == stack[-1]:
            stack.pop()
            continue
        stack.append(s)
    
    return ''.join(stack)

 

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

 

 

728x90
반응형

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

[Python] StrSymmetryPoint  (0) 2021.07.18
[Python] FirstUnique  (0) 2021.07.15
[Python] 8.1. Dominator  (0) 2021.04.05
[Python] 7.3. Nesting  (0) 2021.03.30
[Python] 7.2. Fish  (0) 2021.03.29

댓글