๐ฉ๐ปโ๐ป๋ฌธ์ ๋งํฌ
[์ธํ๋ฐ ์น์
6] ๊ดํธ๋ฌธ์์ ๊ฑฐ (Javascript)
์ ๋ฃ ๊ฐ์์ธ ๊ด๊ณ๋ก ๋ฌธ์ ์ค๋ช
์ ์๋ตํฉ๋๋ค.
โ๏ธIdea Sketch
2021-06-26
1. x === '('์ธ ๊ฒฝ์ฐ stack.push(x);
2. x === ')'์ธ ๊ฒฝ์ฐ stack.pop();
3. x๊ฐ ๋ฌธ์์ด๋ฉด์ stack.length === 0 ์ธ ๊ฒฝ์ฐ res += x;
for (let x of str) {
if (x === '(') stack.push(x);
else if (x === ')') stack.pop();
else if (stack.length === 0) res += x;
}
โ๏ธ์์ค์ฝ๋
2021-06-26
function solution(str) {
let res = '';
let stack = [];
for (let x of str) {
if (x === '(') stack.push(x);
else if (x === ')') stack.pop();
else if (stack.length === 0) res += x;
}
return res;
}