본문 바로가기

Backend/Algorithm

22. Stack - 짝지어 제거하기

 

 

 

코드

 

import java.util.Stack;
class Solution{
    public int solution(String s){
    Stack<Character> stack = new Stack<Character>();
	        
			for(char c : s.toCharArray()) { 
				if(!stack.empty() && stack.peek() == c) {
					stack.pop();
				}else{
					stack.push(c);
				}
			}
		    int solution = stack.empty() ? 1 : 0;
            return solution;

	}
}

 

 

 

접근방법