๐ ์ฝ๋ฉํ
์คํธ/๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 2 : ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ
deep_lee
2023. 4. 3. 15:47
https://school.programmers.co.kr/learn/courses/30/lessons/12973
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
Stack ์ฌ์ฉ.
s์ ๋ฌธ์๋ฅผ ํ๋์ฉ ์ถ๊ฐํ๋ฉด์ Stack์ ์ต์๋จ๊ฐ์ด๋ ๊ฐ์ผ๋ฉด pop ํ๊ณ ์๋๋ฉด add ํ๋ค.
import java.util.*;
class Solution
{
public int solution(String s)
{
int answer = 0;
Stack<Character> stack = new Stack<>();
int index = 0;
stack.add(s.charAt(index++));
while(index<s.length()){
char ch = s.charAt(index++);
if(!stack.empty() && stack.peek() == ch) stack.pop();
else stack.push(ch);
}
return stack.isEmpty() ? 1 : 0;
}
}