문제: https://school.programmers.co.kr/learn/courses/30/lessons/12973
전에 괄호 문제를 풀어둬서 문제를 보자마자 스택으로 풀면 되겠군 하고 풀었는데 맞았음.
별달리 어려운 점은 없었기에 풀이만 남긴다.
풀이
import java.util.*;
class Solution
{
public int solution(String s)
{
int answer = -1;
Stack<Character> stack = new Stack<>();
char next_peek = s.charAt(0);
for(char c : s.toCharArray()){
if(stack.isEmpty()){
//비어있다면 이전을 검사하지 않고 넣는다.
next_peek = c;
stack.push(c);
}else {
//비어있지 않다면 검사한다.
stack.push(c);
//넣은 뒤 중복이면 pop
if(stack.peek() == next_peek){
stack.pop();
stack.pop();
if(!stack.isEmpty()) next_peek = stack.peek();
}else {
next_peek = c;
}
}
}
answer = (stack.isEmpty())? 1 : 0;
return answer;
}
}