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

동민·2021년 3월 11일
import java.util.Stack;

// 짝지어 제거하기 - 2017 팁스타운
public class RemovePairChar {
	
	// StringBuilder 를 사용하면 시간초과 뜸
	public int solution(String s) {

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

		for (char ele : s.toCharArray()) {

			if (stack.isEmpty()) {
				stack.push(ele);
				continue;
			}
			if (stack.peek() == ele) {
				stack.pop();
			} else {
				stack.push(ele);
			}

		}

		return stack.isEmpty() ? 1 : 0;

	}

	public static void main(String[] args) {

		RemovePairChar s = new RemovePairChar();
		System.out.println(s.solution("baabaa")); // 1
		System.out.println(s.solution("cdcd")); // 0
		System.out.println(s.solution("caab")); // 0

	}

}
profile
BE Developer

0개의 댓글