240515

๋ฌธ์ œ : ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋‚œ์ด๋„ : Lv.1 ์„ค๋ช… :

๊ด„ํ˜ธ๊ฐ€ ๋ฐ”๋ฅด๊ฒŒ ์ง์ง€์–ด์กŒ๋‹ค๋Š” ๊ฒƒ์€ '(' ๋ฌธ์ž๋กœ ์—ด๋ ธ์œผ๋ฉด ๋ฐ˜๋“œ์‹œ ์ง์ง€์–ด์„œ ')' ๋ฌธ์ž๋กœ ๋‹ซํ˜€์•ผ ํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด

  • "()()" ๋˜๋Š” "(())()" ๋Š” ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ์ž…๋‹ˆ๋‹ค.

  • ")()(" ๋˜๋Š” "(()(" ๋Š” ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ด„ํ˜ธ์ž…๋‹ˆ๋‹ค. '(' ๋˜๋Š” ')' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฌธ์ž์—ด s๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ์ด๋ฉด true๋ฅผ return ํ•˜๊ณ , ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ด„ํ˜ธ์ด๋ฉด false๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

ํ’€์ด

function solution(s){
    var answer = 0

    for(let i = 0; i < s.length; i++) {
        s[i] === "(" ? answer += 1 : answer -= 1

        if(answer < 0) break
    }

    return answer === 0 ? true : false
}

Last updated