-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecodeString.java
36 lines (34 loc) · 1.15 KB
/
DecodeString.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
class DecodeString {
public String decodeString(String s) {
/*
Runtime: 1 ms, faster than 84.91% of Java online submissions for Decode String.
Memory Usage: 40.3 MB, less than 87.19% of Java online submissions for Decode String.
*/
if (s == null || s.length() == 0) {
return null;
}
Stack<Integer> inStack = new Stack<>();
Stack<StringBuilder> strStack = new Stack<>();
StringBuilder cur = new StringBuilder();
int mult = 0;
for (char ch : s.toCharArray()) {
if (Character.isDigit(ch)) {
mult = mult * 10 + (ch - '0');
} else if (ch == '[') {
inStack.push(mult);
mult = 0;
strStack.push(cur);
cur = new StringBuilder();
} else if (ch == ']') {
StringBuilder tmp = cur;
cur = strStack.pop();
for (int i = inStack.pop(); i > 0; i--) {
cur.append(tmp);
}
} else {
cur.append(ch);
}
}
return cur.toString();
}
}