# 괄호 변환 - 프로그래머스
def check(string):
li = []
for s in string:
if s == '(':
li.append('(')
else:
if li == []:
return False
else:
li.pop()
return True
def split(p):
idx = 0
temp = []
while True:
temp.append(p[idx])
idx += 1
if temp.count('(') == temp.count(')'):
break
return p[:idx], p[idx:]
def reverse(u):
re_s = ""
for i in u:
if i == '(':
re_s += ')'
else:
re_s += '('
return re_s
def solution(p):
ret = ""
while p != "":
u, p = map(str, split(p))
if check(u) == True:
ret += u
else:
ret += '(' + solution(p) + ')' + reverse(u[1:-1])
break
return ret