Description
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 = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
Solution
DFS
这道题让我们把一个按一定规则编码后的字符串解码成其原来的模样,编码的方法很简单,就是把重复的字符串放在一个中括号里,把重复的次数放在中括号的前面,注意中括号里面有可能会嵌套中括号,这题可以用递归和迭代两种方法来解。
我们首先来看递归的解法,我们把一个中括号中的所有内容看做一个整体,一次递归函数返回一对中括号中解码后的字符串。给定的编码字符串实际上只有四种字符,数字,字母,左中括号,和右中括号。那么我们开始用一个变量i从0开始遍历到字符串的末尾,由于左中括号都是跟在数字后面,所以我们首先遇到的字符只能是数字或者字母,如果是字母,我们直接存入结果中,如果是数字,我们循环读入所有的数字,并正确转换,那么下一位非数字的字符一定是左中括号,我们指针右移跳过左中括号,对之后的内容调用递归函数求解,注意我们循环的停止条件是遍历到末尾和遇到右中括号,由于递归调用的函数返回了子中括号里解码后的字符串,而我们之前把次数也已经求出来了,那么循环添加到结果中即可,参见代码如下:
注意递归需要返回当前层处理到哪个index之前,否则上层无法得知改从哪里开始继续处理。换言之,上层需要知道下层处理到什么地方了,即’]'的位置。我这里返回的idx代表该从哪里开始,即']'的后一个位置。
class Solution {
public String decodeString(String s) {
return decode(s.toCharArray(), 0).str;
}
public Pair decode(char[] arr, int start) {
StringBuilder sb = new StringBuilder();
int i = start;
while (i < arr.length) {
if (Character.isDigit(arr[i])) {
int count = 0;
while (Character.isDigit(arr[i])) {
count = 10 * count + (arr[i++] - '0');
}
// arr[i] should be '[' now
Pair sub = decode(arr, i + 1);
while (count-- > 0) {
sb.append(sub.str);
}
i = sub.idx;
} else if (arr[i] == ']') { // consume one ']'
++i;
break;
} else {
sb.append(arr[i++]);
}
}
return new Pair(sb.toString(), i);
}
class Pair {
String str;
int idx;
public Pair(String str, int idx) {
this.str = str;
this.idx = idx;
}
}
}
Stack, time O(n), space O(n)
用Stack做更赏心悦目一点。
class Solution {
public String decodeString(String s) {
if (s == null || s.isEmpty()) {
return s;
}
Stack<Integer> countStack = new Stack<>(); // peek() is curr level count
Stack<String> strStack = new Stack<>(); // peek() is prev level str
int i = 0;
String res = ""; // store curr level str
while (i < s.length()) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
int count = 0;
// count might be over 1-digit length
while (i < s.length() && Character.isDigit(s.charAt(i))) {
count = 10 * count + s.charAt(i) - '0';
++i;
}
countStack.push(count);
} else if (c == '[') { // enter a new level, push and clear res
strStack.push(res);
res = "";
++i;
} else if (c == ']') { // curr level ends, merge it into prev level
StringBuilder sb = new StringBuilder();
sb.append(strStack.pop());
int count = countStack.pop();
while (count-- > 0) {
sb.append(res);
}
res = sb.toString();
++i;
} else {
res += c;
++i;
}
}
return res;
}
}