-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringDecoder.java
73 lines (65 loc) · 2.27 KB
/
StringDecoder.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.sean.stack;
import java.util.Stack;
/***
* 394. Decode String
*/
public class StringDecoder {
private Stack<String> stack = new Stack<>();
// stack ops involve digits and chars
// * [ -> digit : keep pushing in
// * ] -> strs = chars * digit, push(strs)
// * char -> pushed in
public String decodeString(String s) {
int len = s.length();
int i = 0;
StringBuilder numBuilder = new StringBuilder();
while (i < len) {
char ch = s.charAt(i);
if (Character.isDigit(ch)) {
numBuilder.append(ch);
} else if (ch == '[') {
if (numBuilder.length() > 0) {
// int multipler = Integer.parseInt(numBuilder.toString());
stack.push(numBuilder.toString());
numBuilder = new StringBuilder();
}
} else if (ch == ']') {
StringBuilder builder = new StringBuilder();
String str = stack.pop();
int n = Integer.parseInt(stack.pop());
for (int j = 0; j < n; j++) {
builder.append(str);
}
if (!stack.isEmpty()) {
String top = stack.peek();
if (Character.isLetter(top.charAt(0))) {
stack.pop();
stack.push(builder.insert(0, top).toString());
} else {
stack.push(builder.toString());
}
} else {
stack.push(builder.toString());
}
} else { // letter
if (!stack.isEmpty()) {
String top = stack.peek();
if (Character.isDigit(top.substring(0, 1).charAt(0))) {
stack.push(ch + "");
} else {
stack.pop();
stack.push(top + ch);
}
} else {
stack.push(ch + "");
}
}
++i;
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.toString();
}
}