https://programmers.co.kr/learn/courses/30/lessons/12973
✅ Solution
- answer의 길이가 0이라면 문자를 그냥 넣어준다.
- 만약 1이 아니라면 answer의 가장 마지막 요소와 alpha를 비교하여
- 같다면 짝지어 제거할 수 있으므로 answer.pop()을 이용하여 제거해준다.
- 다르다면 alpha를 answer에 넣어준다.
- answer의 길이가 0이라면 모두 짝지어 없어진 것을 의미하므로 1을 리턴하고, 아니라면 0을 리턴한다.
✅ Code
def solution(s):
answer = []
for alpha in s:
if not answer:
answer.append(alpha)
else:
if answer[-1] == alpha:
answer.pop()
else:
answer.append(alpha)
return 1 if len(answer) == 0 else 0
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - 배달 (Python) (0) | 2022.06.09 |
---|---|
[프로그래머스] Level 2 - 괄호 회전하기 (Python) (0) | 2022.06.09 |
[프로그래머스] Level 2 - 타겟 넘버 (Python) (0) | 2022.06.04 |
[프로그래머스] Level 2 - 더 맵게 (Python) (0) | 2022.06.03 |
[프로그래머스] Level 2 - 주차 요금 계산 (Python) (0) | 2022.05.24 |