알고리즘/programmers
[Programmers] 짝지어 제거하기
akjfal
2021. 6. 24. 11:46
import java.util.Stack;
class Solution{
public int solution(String s){
int answer = 1;
int length = s.length();
Stack<Character> stack = new Stack<>();
stack.push(s.charAt(0));
int idx = 1;
while(idx < length){
char alpha = s.charAt(idx);
if(stack.isEmpty()){
stack.push(alpha);
}else if(alpha == stack.peek()){
stack.pop();
}else{
stack.push(alpha);
}
idx++;
}
if(!stack.isEmpty())
answer = 0;
return answer;
}
}
시간초과 나서 새로운 방법이 있는줄알고 한참 헤맸는데 그냥 조건을 조금더 주면되는 거였다...