Skip to content

Commit 7cbc3a1

Browse files
committed
[PGS] 짝지어 제거하기 / Level 2 / 50분(성공)
1 parent 4433595 commit 7cbc3a1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def solution(s):
2+
stack = []
3+
for c in s:
4+
# 스택이 비어있는 경우
5+
if len(stack) == 0:
6+
stack.append(c)
7+
# 가장 최근 문자와 비교
8+
elif stack[-1] == c:
9+
stack.pop()
10+
# 문자가 같지 않아서, 삭제할 필요가 없을 경우
11+
else:
12+
stack.append(c)
13+
14+
# 짝이 다 맞았다면 스택에 남는 것이 없기 때문에 비어있고, 짝이 맞지 않았다면 스택에 문자가 남아있다.
15+
# 따라서 스택의 길이가 0인지 비교
16+
if len(stack) == 0:
17+
return 1
18+
else:
19+
return 0

0 commit comments

Comments
 (0)