https://programmers.co.kr/learn/courses/30/lessons/60058
def seperation(p):
u = ''
v = ''
left = 0
right = 0
for idx,i in enumerate(p):
if i =="(":
left +=1
u += "("
elif i == ")":
right +=1
u += ")"
if left == right:
v = p[idx+1:]
break
return u,v
def check(p):
flag = False
left = 0
right = 0
for i in p:
if left < right:
return False
if i == ')':
right +=1
elif i == '(':
left +=1
else:
return True
return tmp
def solution(p):
if check(p)== True:
return p
answer = ''
u,v=seperation(p)
flag=check(u)
if flag == True:
return u + solution(v)
elif flag == False:
tmp = "(" + solution(v) +")"
for t in range(1,len(u)-1):
if u[t] == "(":
tmp += ")"
elif u[t] == ")":
tmp += "("
return tmp