Write a program that removes all characters between parentheses ( ) in the input string and prints only the remaining characters.
(A(BC)D)EF(G(H)(IJ)K)LM(N)
Only the remaining characters are printed.
EFLM
lastIndexOf()
function solution(s) {
let answer = '';
for (let x of s) {
answer += x;
if (x === ')') {
const pairIndex = answer.lastIndexOf('(');
answer = answer.slice(0, pairIndex);
}
}
return answer;
}
while
loopfunction solution(s) {
let answer,
stack = [];
for (let x of s) {
if (x === ')') {
while (stack.pop() !== '(');
} else stack.push(x);
}
answer = stack.join('');
return answer;
}