programmers_짝지어 제거하기_java
2023. 4. 3. 13:59ㆍAlgorithm/Programmers
728x90
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<>();
int sLength = s.length();
for(int i = 0; i<sLength; i++){
if(!stack.isEmpty() && s.charAt(i) == stack.peek()){
stack.pop();
}else{
stack.push(s.charAt(i));
}
}
if(stack.isEmpty()){
answer = 1;
}else{
answer = 0;
}
return answer;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_점프와 순간 이동_java (0) | 2023.04.03 |
---|---|
programmers_예상 대진표_java (1) | 2023.04.03 |
programmers_다음 큰 숫자_java (0) | 2023.04.03 |
programmers_피보나치 수_java (0) | 2023.04.03 |
programmers_구명보트_java (0) | 2023.04.03 |