Jam's story
[프로그래머스] 짝지어 제거하기 본문
ArrayList로 풀이하니 효용성 실패
import java.util.*;
class Solution
{
public int solution(String s)
{
ArrayList<String> array = new ArrayList<>(Arrays.asList(s.split("")));
int i = 0;
while(i < array.size() - 1) {
if (array.get(i).equals(array.get(i+1))) {
array.remove(i);
array.remove(i);
i = 0;
continue;
}
i++;
}
if (array.size() == 0) return 1;
else return 0;
}}
Stack 으로 풀기
import java.util.*;
class Solution
{
public int solution(String s)
{
int answer=0;
Stack<Character> st=new Stack<>();
st.push(s.charAt(0));
for(int i=1; i<s.length(); i++){
if(!st.isEmpty()&&st.peek()==s.charAt(i)){
st.pop();
}else {
st.push(s.charAt(i));
}
}
answer=st.isEmpty()?1:0;
return answer;
}
}
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 문자열 다루기 기본 (0) | 2022.08.03 |
---|---|
[프로그래머스] 뉴스 클러스터링 (0) | 2022.07.28 |
[프로그래머스] 더맵게 (0) | 2022.07.18 |
[프로그래머스] 오픈채팅방 (0) | 2022.07.14 |
[프로그래머스] 문자열 압축 (0) | 2022.07.13 |