https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV14_DEKAJcCFAYD&categoryId=AV14_DEKAJcCFAYD&categoryType=CODE&problemTitle=&orderBy=PASS_RATE&selectCodeLang=ALL&select-1=3&pageSize=10&pageIndex=3
Stack์ ์ด์ฉํด์ ํ๋ฉด ์ฝ๊ฒ ํ๋ฆฐ๋ค.
import java.util.Scanner;
import java.util.Stack;
class Solution{
public static void main(String args[]) throws Exception{
Scanner kb = new Scanner(System.in);
int t = 10;
for (int test_case = 1; test_case <= t; test_case++) {
int n = kb.nextInt();
String s = kb.next();
String[] arr = s.split(""); // ์ซ์๋ฅผ ๋ฐฐ์ด๋ก
Stack<String> stack = new Stack<>();
for(int i=0; i<arr.length; i++){
// ์คํ์ด ๋น์ด์๊ฑฐ๋, ์คํ peek์ ๋ค๋ฅธ ๋ฌธ์๋ผ๋ฉด push
if(stack.isEmpty()||!arr[i].equals(stack.peek())){
stack.push(arr[i]);
}
else{ // ์คํ peek๊ฐ ํ์ฌ ๋ฌธ์์ ๊ฐ๋ค๋ฉด pop
stack.pop();
}
}
String answer = "";
// ์ ๋ต answer์ ์์๋๋ก ์ถ๊ฐ
for (int i = 0; i < stack.size(); i++)
answer += stack.get(i);
System.out.println("#" + test_case + " " + answer);
}
}
}
'๐ ์ฝ๋ฉํ ์คํธ > ๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SWEA][JAVA]D3 : ๋์๋ฌผ ์ํํ๊ธฐ (0) | 2022.11.08 |
---|---|
[SWEA][JAVA]D3 : ๋์ฒ ์ด์ ํ๋ก๊ทธ๋๋ฐ ๋ํ (0) | 2022.11.08 |
[SWEA][JAVA]D3 : [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 4์ผ์ฐจ - ๊ฑฐ๋ญ ์ ๊ณฑ (0) | 2022.11.07 |
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : 2์ฐจ์์ผ๋ก ๋ง๋ค๊ธฐ (0) | 2022.11.07 |
[SWEA][JAVA] D3 : ํ์์ผ๊น ์์์ผ๊น (0) | 2022.11.05 |