Deep_Dev
article thumbnail

https://school.programmers.co.kr/learn/courses/30/lessons/12909

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 


 

์˜ค๋žœ๋งŒ์— Stack๋งŒ ์จ์„œ ์—ฐ์Šตํ•œ๋“ฏํ•˜๋‹ค.

 

import java.util.*;

class Solution {
    boolean solution(String s) {
         boolean answer = true;

        Stack<Character> stack = new Stack<>();

        for(int i=0; i<s.length(); i++){
            if(s.charAt(i) == '(') stack.add(s.charAt(i));
            else {
                if(stack.isEmpty()) answer = false;
                else{
                    stack.pop();
                }
            }
        }
        if(!stack.empty()) answer=false;
        return answer;
    }
}