Deep_Dev

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

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

 

 


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);
        }
    }
}