- Published on
Codility - Nesting
- Author
- Name
- yceffort
Nesting
๋ฌธ์
(
์ )
๋ก ์ด๋ฃจ์ด์ง ๋ฌธ์์ด์ด ์๋ค. ์ด ๋ฌธ์์ด์ (
)
์ง์ด ๋ง๊ฒ ์ด๋ฃจ์ด์ ธ ์๋์ง ํ์ธํ๋ผ.
ํ์ด
function solution(S) {
const split = S.split('')
const stack = []
for (let i of split) {
// ์ฌ๋ ๊ดํธ๋ผ๋ฉด ์คํ์ ํ๋์ฉ ๋ฃ๋๋ค
if (i === '(') {
stack.push(true)
// ๋ซ๋ ๊ดํธ๋ผ๋ฉด
} else {
// ๋ซ๋๊ดํธ์ธ๋ฐ ์ฌ๋๊ดํธ๊ฐ ์๋ค๋ฉด ์ด๋ฏธ ๊ธ๋ฌ๋จน์๋ค
if (stack.length === 0) {
return 0
// ํ๋ ์์ผ๋ฉด ๊บผ๋ธ๋ค
} else {
stack.pop()
}
}
}
// ์คํ์ด ๊น๋ํ๊ฒ ๋น์ด์์ผ๋ฉด 1์ ๋ฆฌํดํ๋ค.
return stack.length === 0 ? 1 : 0
}