일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Queue
- import re
- sql
- counter
- programmers
- 코딩
- 파이썬
- level4
- 코테
- collections
- join
- coding test
- coding
- time complexity
- python
- lambda
- 시간복잡도
- Level2
- 완전탐색
- BFS
- Stack
- 프로그래머스
- CodingTest
- itertools
- mysql
- 연습문제
- 조합
- 데이터분석
- lv4
- 코딩테스트
- Today
- Total
ror_coding
[Programmers] 올바른 괄호 - 12909, 짝지어 제거하기 - 12973 본문
728x90
stack.pop() 사용하기 ! index 조절 때문에 고민 좀 했다..
올바른 괄호, 짝지어 제거하기 둘 다 같은 문제라 같이 기록한다.
Question : 올바른 괄호 - 12909
'(' 또는 ')' 로만 이루어진 문자열 s가 주어졌을 때, 문자열 s가 올바른 괄호이면 true를 return 하고, 올바르지 않은 괄호이면 false를 return 하는 solution 함수를 완성해 주세요.
Code
def solution(s):
stack = []
for i in s:
if not stack : stack.append(i); continue
if stack[-1] =='(' and i==')' : stack.pop()
else : stack.append(i)
return False if stack else True
![](https://t1.daumcdn.net/keditor/emoticon/face/large/012.png)
now me
On my github
Question : 짝지어 제거하기 - 12973
짝지어 제거하기는, 알파벳 소문자로 이루어진 문자열을 가지고 시작합니다. 먼저 문자열에서 같은 알파벳이 2개 붙어 있는 짝을 찾습니다. 그다음, 그 둘을 제거한 뒤, 앞뒤로 문자열을 이어 붙입니다. 이 과정을 반복해서 문자열을 모두 제거한다면 짝지어 제거하기가 종료됩니다. 문자열 S가 주어졌을 때, 짝지어 제거하기를 성공적으로 수행할 수 있는지 반환하는 함수를 완성해 주세요. 성공적으로 수행할 수 있으면 1을, 아닐 경우 0을 리턴해주면 됩니다.
Code : 짝지어 제거하기 - 12973
def solution(s):
stack = []
for i in s:
if not stack : stack.append(i); continue
if stack[-1] == i : stack.pop(); continue
stack.append(i)
return 0 if stack else 1
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/014.gif)
now me
On my github
728x90
'Algorithm > Python' 카테고리의 다른 글
[Programmers] 멀리 뛰기 - 12914 (1) | 2024.10.31 |
---|---|
[Programmers] 점프와 순간 이동 - 12980 (0) | 2024.10.28 |
[Programmers] 신고 결과 받기 - 92334 (0) | 2024.10.28 |
[Programmers] 개인정보 수집 유효기간 - 150370 (1) | 2024.10.26 |
[Programmers] 바탕화면 정리 - 161990 (0) | 2024.10.26 |