-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2.java
More file actions
46 lines (43 loc) · 1.57 KB
/
Copy pathQuestion2.java
File metadata and controls
46 lines (43 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package practice3_struct;
import java.util.*;
public class Question2 {
// 겹쳐진 압축 해제
public String solution(String s){
String answer = "";
Stack<String> st = new Stack<>();
for(Character x : s.toCharArray()){
if(x == ')'){
String tmp = "";
while(!st.empty()){
String c = st.pop();
if(c.equals("(")){
String num = "";
while(!st.empty() && Character.isDigit(st.peek().charAt(0))){
num = st.pop() + num;
}
String res = "";
int cnt = 0;
if(num.equals("")) cnt = 1;
else cnt = Integer.parseInt(num);
for(int i = 0; i < cnt; i++) res += tmp;
st.push(res);
break;
}
tmp = c + tmp;
}
}
else st.push(String.valueOf(x));
}
for(String x : st) answer += x;
return answer;
}
// 괄호가 있으면 무조건 스택!! , 음수 양수 짝꿍,
public static void main(String[] args){
Question2 T = new Question2();
System.out.println(T.solution("3(a2(b))ef"));
System.out.println(T.solution("2(ab)k3(bc)"));
System.out.println(T.solution("2(ab3((cd)))"));
System.out.println(T.solution("2(2(ab)3(2(ac)))"));
System.out.println(T.solution("3(ab2(sg))"));
}
}