const isRight=(p)=>{
let stack=[];
for (let i=0;i<p.length;i++){
if (p[i]==='('){
stack.push('(');
}else{
if (stack.length===0){
return false;
}
stack.pop();
}
}
return true;
}
function solution(p) {
var answer = '';
let open=0;
let close=0;
if (p.length<=0){
return '';
}
for (let i=0;i<p.length;i++){
if (p[i]==='('){
open+=1;
}else{
close+=1;
}
// 올바른가
if (open===close){
if (isRight(p.slice(0, i+1))){
answer=p.slice(0, i+1)+solution(p.slice(i+1));
return answer;
}else{
answer='('+solution(p.slice(i+1))+')';
for (let j=1;j<i;j++){
if (p[j]==='('){
answer=answer+')';
}else{
answer=answer+'(';
}
}
return answer
}
}
}
}