Restore bracket sequence
// Restore bracket sequence
function restore(str) {
let arr = [];
let counter = 0;
for(let i=0; i<str.length; i++) {
if (str[i]=='(') {
arr.push('(');
counter++;
} else { // str[i]==')'
counter--;
if (counter < 0) {
arr.push('(');
}
arr.push(')')
}
}
for(let i=0; i<counter; i++) {
arr.push(')');
}
return arr.join('');
}
console.log('===> start');
console.log(restore(')))'));