forked from seanprashad/leetcode-patterns
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path394_Decode_String.java
39 lines (32 loc) · 1.11 KB
/
394_Decode_String.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
classSolution {
publicStringdecodeString(Strings) {
if (s == null || s.isEmpty()) {
return"";
}
Stack<StringBuilder> sbStk = newStack<>();
Stack<Integer> multipliers = newStack<>();
StringBuilderresult = newStringBuilder();
intmultiplier = 0;
for (inti = 0; i < s.length(); i++) {
charc = s.charAt(i);
if (Character.isDigit(c)) {
multiplier = multiplier * 10 + c - '0';
} elseif (c == '[') {
sbStk.push(result);
multipliers.push(multiplier);
result = newStringBuilder();
multiplier = 0;
} elseif (c == ']') {
StringBuilderpreviousString = sbStk.pop();
intprevMultiplier = multipliers.pop();
for (intj = 0; j < prevMultiplier; j++) {
previousString.append(result);
}
result = previousString;
} else {
result.append(c);
}
}
returnresult.toString();
}
}