[Programmers] 2017 팁스타운 - 짝지어 제거하기

zzenee·2022년 5월 8일
0

Algorithm&Coding-test

목록 보기
7/30
post-thumbnail

Problem

Code

import java.util.*;
class Solution {
    public int solution(String s) {
        int answer = -1;
        Stack<Character> stack = new Stack<>();
        for (char ch: s.toCharArray()) {
            if (stack.isEmpty()) stack.push(ch);
            else {
                char tmp = stack.pop();
                if (tmp != ch) {
                    stack.push(tmp);
                    stack.push(ch);
                }
            }
        }
        if (stack.size() == 0) answer = 1;
        else answer = 0;

        return answer;
    }
}
profile
꾸준히

0개의 댓글