Given an encoded string, return it's decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].
Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "xyz3[a12[cb]]", return "....."
Solution1:two Stacks (count, result)
思路:到数字时,将原来的res存入res_stack,数字存入count_stack,res重置 ,继续处理后序。碰到]时,将res_stack.pop() + res * count_stack.pop() times,继续处理后序。
Time Complexity: O(N) Space Complexity: O(N)
Solution2:Recursive
参考:https://discuss.leetcode.com/topic/57228/0ms-simple-c-solution
Time Complexity: O(N) Space Complexity: O(N) 递归缓存
Solution1 Code:
public class Solution {
public String decodeString(String s) {
String res = "";
Stack<Integer> countStack = new Stack<>();
Stack<String> resStack = new Stack<>();
int idx = 0;
while (idx < s.length()) {
if (Character.isDigit(s.charAt(idx))) {
int count = 0;
while (Character.isDigit(s.charAt(idx))) {
count = 10 * count + (s.charAt(idx) - '0');
idx++;
}
countStack.push(count);
}
else if (s.charAt(idx) == '[') {
resStack.push(res);
res = "";
idx++;
}
else if (s.charAt(idx) == ']') {
StringBuilder temp = new StringBuilder (resStack.pop());
int repeatTimes = countStack.pop();
for (int i = 0; i < repeatTimes; i++) {
temp.append(res);
}
res = temp.toString();
idx++;
}
else {
res += s.charAt(idx++);
}
}
return res;
}
}
Solution2 Code:
public class Solution {
public String decodeString(String s) {
StringBuilder result = decode(s, new int[] {0});
return result.toString();
}
// start: //so that it can be changed. (like reference)
private StringBuilder decode(String s, int[] start) {
StringBuilder result = new StringBuilder();
while(start[0] < s.length() && s.charAt(start[0]) != ']') {
if(Character.isDigit(s.charAt(start[0]))) {
int count = 0;
while (Character.isDigit(s.charAt(start[0]))) {
count = 10 * count + (s.charAt(start[0]) - '0');
start[0]++;
}
start[0]++; // ']'
StringBuilder post = decode(s, start);
start[0]++; // ']'
while(count > 0) {
result.append(post);
count--;
}
}
else {
result.append(s.charAt(start[0]));
start[0]++;
}
}
return result;
}
}
Round1
class Solution {
public String decodeString(String s) {
Deque<String> stack_str = new ArrayDeque<>();
Deque<Integer> stack_count = new ArrayDeque<>();
int i = 0;
StringBuilder str = new StringBuilder("");
while(i < s.length()) {
if(Character.isDigit(s.charAt(i))) {
int num = 0;
while(Character.isDigit(s.charAt(i))) {
num = num * 10 + s.charAt(i) - '0';
i++;
}
stack_count.push(num);
}
else if(s.charAt(i) == '[') {
stack_str.push(str.toString());
str = new StringBuilder("");
i++;
}
else if(s.charAt(i) == ']') {
StringBuilder prev = new StringBuilder(stack_str.pop());
int times = stack_count.pop();
for(int t = 0; t < times; t++) {
prev.append(str);
}
str = prev;
i++;
}
else {
// regular string
str.append(s.charAt(i));
i++;
}
}
return str.toString();
}
}