LeetCode 394. Decode String

原題連接在這裏:https://leetcode.com/problems/decode-string/#/descriptionhtml

題目:git

Given an encoded string, return it's decoded string.app

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.ide

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.post

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].ui

Examples:url

s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

題解:spa

當s的長度小於4的時候能夠直接返回s, 由於縮寫的s格式k[encoded_string]最小長度是4.code

利用兩個Stack完成DFS. stringStk 保存遇到縮寫前上層的String值, intStk保存當前層應該repeat的次數.orm

遇到數字, 就說明遇到縮寫了, 把須要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 }

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 }

相似Brace Expansion.

相關文章
相關標籤/搜索