문제풀이
이문제는 스택과 큐를 이용해서 해결하는 문제다. 풀이에 사용된 방법은 닫는 괄호를 만나면 여는 괄호를 만날 때까지 pop을 시킨다.
pop 되지 않은 문자열을 괄호 안에 없다고 판단하여 join 하여 return 한다.
| function solution(str) { |
| const stack = []; |
| for (x of str) { |
| if (x === ")") { |
| while (stack.pop() !== "("); |
| } else stack.push(x); |
| } |
| |
| return stack.join(""); |
| } |
| |
| const str = "(A(BC)D)EF(G(H)(IJ)K)LM(N)"; |
| console.log(solution(str)); |
Github
https://github.com/JongyunHa/algorithm-levelup/blob/main/javascript/dataStructure/removeParenthesisChar.js
GitHub - JongyunHa/algorithm-levelup: Algorithm Study
Algorithm Study. Contribute to JongyunHa/algorithm-levelup development by creating an account on GitHub.
github.com
댓글을 사용할 수 없습니다.