Given an encoded string, return its 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]
.
Example 1:
Input: s = "3[a]2[bc]" Output: "aaabcbc"
Example 2:
Input: s = "3[a2[c]]" Output: "accaccacc"
Example 3:
Input: s = "2[abc]3[cd]ef" Output: "abcabccdcdcdef"
Example 4:
Input: s = "abc3[cd]xyz" Output: "abccdcdcdxyz"
Constraints:
1 <= s.length <= 30
s
consists of lowercase English letters, digits, and square brackets'[]'
.s
is guaranteed to be a valid input.- All the integers in
s
are in the range[1, 300]
.
字符串解码。
给定一个经过编码的字符串,返回它解码后的字符串。
编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。
你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。
此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/decode-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是用stack。需要用到两个stack,numStack记录所有出现的数字,strStack记录所有出现的string。
当遇到字母的时候,先用一个StringBuilder res暂时记录下来;
当遇到数字的时候,就放入numStack栈保存;
当遇到左括号的时候就把已经记录下来的放入string stack,这样再遇到右括号需要结算的时候,左括号之前的部分不会丢失;
当遇到右括号的时候,就从 strStack 中pop出一个元素,和 numStack.pop() 个 res 连起来,然后作为新的res,等待下一次被塞进stack里,或者被和stack里的元素连接起来;
时间O(n)
空间O(n)
Java实现
1 class Solution { 2 public String decodeString(String s) { 3 // corner case 4 if (s == null || s.length() == 0) { 5 return ""; 6 } 7 8 // normal case 9 int len = s.length(); 10 Stack<Integer> numStack = new Stack<>(); 11 Stack<String> strStack = new Stack<>(); 12 StringBuilder res = new StringBuilder(); 13 for (int i = 0; i < len; i++) { 14 char c = s.charAt(i); 15 if (c == '[') { 16 strStack.push(res.toString()); 17 res = new StringBuilder(); 18 } else if (c == ']') { 19 int num = numStack.pop(); 20 StringBuilder temp = new StringBuilder(strStack.pop()); 21 for (int j = 0; j < num; j++) { 22 temp.append(res); 23 } 24 res = temp; 25 } else if (Character.isDigit(c)) { 26 int num = s.charAt(i) - '0'; 27 while (i + 1 < len && Character.isDigit(s.charAt(i + 1))) { 28 num = num * 10 + s.charAt(i + 1) - '0'; 29 i++; 30 } 31 numStack.push(num); 32 } else { 33 res.append(c); 34 } 35 } 36 return res.toString(); 37 } 38 }
JavaScript实现
1 /** 2 * @param {string} s 3 * @return {string} 4 */ 5 var decodeString = function (s) { 6 // corner case 7 if (s === null || s.length === 0) { 8 return ''; 9 } 10 11 // normal case 12 let numStack = []; 13 let strStack = []; 14 let res = ''; 15 let len = s.length; 16 for (let i = 0; i < len; i++) { 17 let c = s.charAt(i); 18 if (c === '[') { 19 strStack.push(res); 20 res = ''; 21 } else if (c == ']') { 22 let num = numStack.pop(); 23 let temp = strStack.pop(); 24 for (let j = 0; j < num; j++) { 25 temp += res; 26 } 27 res = temp; 28 } else if (!isNaN(c)) { 29 let num = Number(c); 30 while (i + 1 < len && !isNaN(s.charAt(i + 1))) { 31 num = num * 10 + Number(s.charAt(i + 1)); 32 i++; 33 } 34 numStack.push(num); 35 } else { 36 res += c; 37 } 38 } 39 return res; 40 };