๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ(์ž๋ฃŒ๊ตฌ์กฐ-์Šคํƒ) ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/Algorithm (๊ธฐ์ดˆ๋ฌธ์ œํ’€์ด)

์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ(์ž๋ฃŒ๊ตฌ์กฐ-์Šคํƒ)

Daehyunii 2022. 9. 2. 17:49

๋ฌธ์ œ(์ถœ์ฒ˜ : ์ธํ”„๋Ÿฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ๊ฐ•์˜, ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ)

 

๊ด„ํ˜ธ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ์ด๋ฉด “YES", ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ”NO"๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (())() ์ด๊ฒƒ์€ ๊ด„ํ˜ธ์˜ ์Œ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์œ„์น˜ํ•˜๋Š” ๊ฑฐ์ง€๋งŒ, (()()))์€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ๊ฐ€ ์•„๋‹ˆ๋‹ค.

โ–ฃ ์ž…๋ ฅ์„ค๋ช…
์ฒซ ๋ฒˆ์งธ ์ค„์— ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 30์ด๋‹ค.

โ–ฃ ์ถœ๋ ฅ์„ค๋ช…
์ฒซ ๋ฒˆ์งธ ์ค„์— YES, NO๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

โ–ฃ ์ž…๋ ฅ์˜ˆ์ œ 1

(()(()))(()

 

โ–ฃ ์ถœ๋ ฅ์˜ˆ์ œ 1

NO

 

Tip

 

 

๋ฌธ์ œํ’€์ด

//์Šคํƒ์„ ํ™œ์šฉํ•ด์„œ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋‹ต
function solution(string){
    let answer = 'YES';
    let stack = [];

    for(let x of string){
        if(x === '('){
            stack.push(x)
        }else{
            if(stack.length === 0){
                return "NO";
            }
            stack.pop()
        }
    }

    if(stack.length > 0){
        answer = 'NO'
    }
    return answer;
}

let string = '(()(()))(()';
let string2 = '()()()()()';
let string3 = '(()()))';
let string4 = '(()())()(';
let string5 = '(()()()())';
console.log(solution(string5));