[백준 16120] PPAP

Junyoung Park·2022년 3월 11일
0

코딩테스트

목록 보기
230/631
post-thumbnail

1. 문제 설명

PPAP

2. 문제 분석

스택을 통해 뒷부분 4자리를 PPAP와 매칭하면서 pop하자.

3. 나의 풀이

import sys

s = sys.stdin.readline().rstrip()
stack = []
for letter in s:
    stack.append(letter)
    while True:
        if len(stack) >= 4 and stack[-1:-5:-1] == ['P', 'A', 'P', 'P']:
            stack.pop()
            stack.pop()
            stack.pop()
        else: break

if stack == ['P']: print('PPAP')
else: print('NP')
profile
JUST DO IT

0개의 댓글