-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeString.java
More file actions
56 lines (52 loc) · 1.4 KB
/
Copy pathDecodeString.java
File metadata and controls
56 lines (52 loc) · 1.4 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
47
48
49
50
51
52
53
54
55
56
import java.util.Stack;
public class DecodeString {
public String decodeString(String s) {
if(s == null || s.length() == 0) return s;
char[] ch = s.toCharArray();
Stack<Integer> numStack = new Stack<>();
Stack<Character> charStack = new Stack<>();
int k = 0;
boolean isDigit = false;
for(int i = 0; i < ch.length; i++){
if(Character.isDigit(ch[i])) {
k = k * 10 + (ch[i] - '0');
isDigit = true;
continue;
}
if(isDigit){
numStack.push(k);
k = 0;
isDigit = false;
}
if(ch[i] != ']'){
charStack.push(ch[i]);
continue;
}
if(ch[i] == ']'){
StringBuilder sb = new StringBuilder();
while(!charStack.isEmpty() && charStack.peek() != '['){
sb.append(charStack.pop());
}
charStack.pop();
int time = numStack.pop();
String temp = sb.reverse().toString();
while(time-- > 0){
for(int j = 0; j < temp.length(); j++)
charStack.push(temp.charAt(j));
}
}
}
StringBuilder sb = new StringBuilder();
while(!charStack.isEmpty()){
sb.append(charStack.pop());
}
return sb.reverse().toString();
}
public static void main(String[] args) {
DecodeString decode = new DecodeString();
System.out.println(decode.decodeString("3[a2[c]]"));
System.out.println(decode.decodeString("2[abc]3[cd]ef"));
System.out.println(decode.decodeString("10[2[abc]3[cd]ef]"));
System.out.println(decode.decodeString("asdes"));
}
}