01-09 18:44
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_36 ๊ด„ํ˜ธ ๋ฌธ์ž ์ œ๊ฑฐ ๋ณธ๋ฌธ

Algorithm/Java

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_36 ๊ด„ํ˜ธ ๋ฌธ์ž ์ œ๊ฑฐ

miinsun 2022. 1. 5. 19:55

 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์—์„œ ์†Œ๊ด„ํ˜ธ ( ) ์‚ฌ์ด์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋‚จ์€ ๋ฌธ์ž๋งŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

 

 

๐Ÿ”จ ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ -  ์ฒซ ์ค„์— ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

(A(BC)D)EF(G(H)(IJ)K)LM(N)

์ถœ๋ ฅ - ๋‚จ์€ ๋ฌธ์ž๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.

EFLM

 

 

โ€‹

๐Ÿ’ป Solution.java

import java.util.*;

public class Main {

	public String solution(String s) {
		String answer = "";
	
		Stack<String> st = new Stack<>();
		for(char c : s.toCharArray()) {
			if(c == ')') {
				if(st.contains("(")) {
					while(!st.lastElement().equals("(")) {
						st.pop();
					}
					st.pop();
				}
				else
					st.push(")");
			}
			else {
				st.push(Character.toString(c));
			}			
		}
		
		for(int i = 0; i < st.size(); i++) {
			answer += st.get(i);
		}
		
        return answer;
	}
	
	public static void main(String[] args){
		Main main  = new Main();
		Scanner sc =new Scanner(System.in);
		
		String s = sc.next();
		
		System.out.println(main.solution(s));
		
		sc.close();
		return ;
	}
}

 

 

 

Comments