-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecodeString.java
50 lines (45 loc) · 1.57 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package solutions;
import java.util.Stack;
// [Problem] https://leetcode.com/problems/decode-string
class DecodeString {
// Stack
// O(n) time, O(n) space
public String decodeString(String s) {
int num = 0;
Stack<String> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
num = num * 10 + (c - '0');
} else if (c == '[') {
stack.push(String.valueOf(num));
stack.push("[");
num = 0;
} else if (c == ']') {
String substring = "";
while (!stack.empty() && !"[".equals(stack.peek())) {
substring = stack.pop() + substring;
}
stack.pop(); // pop '['
int repeatCount = Integer.parseInt(stack.pop());
String repeatedSubstring = substring.repeat(repeatCount);
stack.add(repeatedSubstring);
} else {
stack.push(String.valueOf(c));
}
}
String result = "";
while (!stack.empty()) {
result = stack.pop() + result;
}
return result;
}
// Test
public static void main(String[] args) {
DecodeString solution = new DecodeString();
String input = "3[a2[c]]";
String expectedOutput = "accaccacc";
String actualOutput = solution.decodeString(input);
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
}
}