728x90
package prog;
import java.util.Stack;
public class 짝지어_제거하기 {
public static void main(String[] args) {
Solution_짝지어_제거하기 s = new Solution_짝지어_제거하기();
System.out.println(s.solution("baabaa"));
System.out.println(s.solution("cdcd"));
}
}
class Solution_짝지어_제거하기
{
public int solution(String s)
{
int answer = -1;
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if(stack.isEmpty()){
stack.add(c);
}
else{
if(stack.peek()==c){
stack.pop();
}
else stack.add(c);
}
}
if(stack.isEmpty()) return 1;
else return 0;
}
}
728x90