sohyeon kim

[Python] 백준 : 괄호 끼워넣기 11899, 스택, stack 본문

Coding Test

[Python] 백준 : 괄호 끼워넣기 11899, 스택, stack

aotoyae 2025. 1. 28. 23:48
728x90
반응형

 

 

📝 문제

첫 번째 줄에 올바르지 않은 괄호열 S가 주어집니다. S의 길이는 1 이상 50 이하입니다.

첫 번째 줄에 S를 올바른 괄호열으로 만들기 위해 앞과 뒤에 붙여야 할 괄호의 최소 개수를 출력합니다. 불가능한 경우는 주어지지 않습니다.

 

🫠 나의 풀이

import sys
sys.stdin = open('input.txt', 'r')
# input = sys.stdin.readline

S = input().strip()
stack = []
cnt = 0

for i in S:
    if i == '(':
        stack.append('(')
    else:
        if stack:
            stack.pop()
        else:
            cnt += 1

print(len(stack) + cnt)

 

🧞‍♂️ 다른 사람의 풀이

import sys
sys.stdin = open('input.txt', 'r')
# input = sys.stdin.readline

S = input().strip()

while '()' in S:
    S = S.replace('()', '')

print(len(S))

 

 

 

🔗 https://www.acmicpc.net/problem/11899

 

 

 

728x90
반응형