-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
45 lines (36 loc) · 1.01 KB
/
Copy pathSolution.java
File metadata and controls
45 lines (36 loc) · 1.01 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
import java.util.ArrayDeque;
class DecodeString{
private String decodeString(String s){
Deque<Character> st= new ArrayDeque<>();
for(char c: s.toCharArray())
st.offer(c);
return decode(st);
}
private String decode(Deque<Character> st){
StringBuilder sb= new StringBuilder();
int num=0;
while(!st.isEmpty()){
char c= st.poll();
if(Character.isDigit(c)){
num = num * 10 + c - '0';
}
else if(c == '['){
String substr= decode(st);
for(int i=0; i<num; i++) sb.append(substr);
num=0;
}
else if(c == ']')
break;
else{
sb.append(c);
}
}
return sb.toString();
}
public static void main(String[] args) {
String s="3[a]2[bc]";
System.out.println(decodeString(s));
// Input: s = "3[a]2[bc]"
// Output: "aaabcbc"
}
}