LeetCode 394. Decode String
原题链接在这里:https://leetcode.com/problems/decode-string/#/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".
题解:
当s的长度小于4的时候可以直接返回s, 因为缩写的s格式k[encoded_string]最小长度是4.
利用两个Stack完成DFS. stringStk 保存遇到缩写前上层的String值, intStk保存当前层应该repeat的次数.
遇到数字, 就说明遇到缩写了, 把需要repeat的次数先保存进inStk中. 把上一层的string保存到stringStk中.
遇到"]"说明缩写结束. 把上一层的string 从stingStk中pop出来,后面append上repeat次数的当前层string.
Time Complexity: O(s.length()). Space: O(s.length()), s是缩写的极致时用了最大的Stack.
AC Java:
1 public class Solution { 2 public String decodeString(String s) { 3 if(s == null || s.length() < 4){ 4 return s; 5 } 6 StringBuilder res = new StringBuilder(); 7 Stack<String> stringStk = new Stack<String>(); 8 Stack<Integer> intStk = new Stack<Integer>(); 9 int index = 0; 10 while(index < s.length()){ 11 if(Character.isDigit(s.charAt(index))){ 12 int n = 0; 13 while(index<s.length() && Character.isDigit(s.charAt(index))){ 14 n = n*10 + (int)(s.charAt(index)-'0'); 15 index++; 16 } 17 intStk.push(n); 18 stringStk.push(res.toString()); 19 res = new StringBuilder(); 20 index++; //跳过"[" 21 }else if(s.charAt(index) == ']'){ 22 String formerRes = stringStk.pop(); 23 StringBuilder sb = new StringBuilder(formerRes); 24 int n = intStk.pop(); 25 while(n-- > 0){ 26 sb.append(res); 27 } 28 res = sb; 29 index++; 30 }else{ 31 res.append(s.charAt(index++)); 32 } 33 } 34 return res.toString(); 35 } 36 }
AC Python:
1 class Solution: 2 def decodeString(self, s: str) -> str: 3 if not s or len(s) == 0: 4 return s 5 6 res = "" 7 stk = [] 8 num = "" 9 10 for c in s: 11 if c.isdigit(): 12 num += c 13 elif c == "[": 14 stk.append([res, num]) 15 res = "" 16 num = "" 17 elif c.isalpha(): 18 res += c 19 elif c == "]": 20 preRes, preNum = stk.pop() 21 res = preRes + res * (int(preNum)) 22 num = "" 23 24 return res
DFS recursion 方法, 终止条件有两个, 遇到了']', 或者到了s的末位.
Time Complexity: O(s.length()). Space: O(s.length()).
AC Java:
1 public class Solution { 2 int i = 0; 3 public String decodeString(String s) { 4 StringBuilder sb = new StringBuilder(); 5 while(i<s.length()){ 6 if(Character.isDigit(s.charAt(i))){ 7 int n = 0; 8 while(i<s.length() && Character.isDigit(s.charAt(i))){ 9 n = n*10 + (int)(s.charAt(i)-'0'); 10 i++; 11 } 12 13 i++; // skip '[' 14 String nested = decodeString(s); 15 16 while(n-- > 0){ 17 sb.append(nested); 18 } 19 }else if(Character.isLetter(s.charAt(i))){ 20 sb.append(s.charAt(i++)); 21 }else if(s.charAt(i) == ']'){ 22 i++; 23 return sb.toString(); 24 } 25 } 26 return sb.toString(); 27 } 28 }
AC Python:
1 class Solution: 2 def __init__(self): 3 self.i = 0 4 5 def decodeString(self, s: str) -> str: 6 if not s or len(s) == 0: 7 return s 8 9 res = "" 10 while self.i < len(s): 11 if s[self.i].isdigit(): 12 num = "" 13 while self.i < len(s) and s[self.i].isdigit(): 14 num += s[self.i] 15 self.i += 1 16 17 self.i += 1 # skip "[" 18 next = self.decodeString(s) 19 res += next * int(num) 20 21 elif s[self.i].isalpha(): 22 res += s[self.i] 23 self.i += 1 24 elif s[self.i] == "]": 25 self.i += 1 26 return res 27 28 return res