코딩 테스트/5. Stack, Queue
Q5 - 2 괄호문자제거
길동이이이잉
2021. 9. 23. 22:03
728x90
반응형
2. 괄호문자제거
* 설명
입력된 문자열에서 소괄호 ( ) 사이에 존재하는 모든 문자를 제거하고 남은 문자만 출력하는 프로그램을 작성하세요.
* 입력
첫 줄에 문자열이 주어진다. 문자열의 길이는 100을 넘지 않는다.
* 출력
남은 문자만 출력한다.
* 예시 입력 1
(A(BC)D)EF(G(H)(IJ)K)LM(N)
* 예시 출력 1
EFLM
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String in1 = in.nextLine();
Stack<Character> stack = new Stack<Character>();
for(char x : in1.toCharArray()) {
if(stack.empty()) {
if(x == '(') {
stack.push(x);
}else {
System.out.print(x);
}
}else {
if(x == '(') stack.push(x);
else if(x == ')') stack.pop();
}
}
in.close();
return;
}
}
import java.util.*;
class Main {
public String solution(String str){
String answer="";
Stack<Character> stack=new Stack<>();
for(char x : str.toCharArray()){
if(x==')'){
while(stack.pop()!='(');
}
else stack.push(x);
}
for(int i=0; i<stack.size(); i++) answer+=stack.get(i);
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.next();
System.out.println(T.solution(str));
}
}
728x90
반응형