Leetcode 394: Decode String

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 394: Decode String相关的知识,希望对你有一定的参考价值。

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

  1 public class Solution {
  2     public string DecodeString(string s)
  3     {
  4         if (s == null || s.Length == 0) return "";
  5         
  6         var counts = new Stack<int>();
  7         var results = new Stack<string>();
  8         
  9         int i = 0;
 10         results.Push("");
 11         
 12         while (i < s.Length)
 13         {
 14             char c = s[i];
 15             
 16             if (Char.IsDigit(c))
 17             {
 18                 int count = (int)c - (int)0;
 19                 while (i + 1 < s.Length && Char.IsDigit(s[i + 1]))
 20                 {
 21                     count = count * 10 + (int)s[i + 1] - (int)0;
 22                     i++;
 23                 }
 24                 
 25                 counts.Push(count);                
 26             }
 27             else if (c == [)
 28             {
 29                 results.Push("");
 30             }
 31             else if (c == ])
 32             {
 33                 string str = results.Pop();
 34                 int count = counts.Pop();
 35                 
 36                 var sb = new StringBuilder();
 37 
 38                 for (int k = 0; k < count; k++)
 39                 {
 40                     sb.Append(str);
 41                 }
 42                 
 43                 results.Push(results.Pop() + sb.ToString());
 44             }
 45             else
 46             {
 47                 results.Push(results.Pop() + c);
 48             }
 49             
 50             i++;
 51         }
 52 
 53         return results.Pop();
 54     }
 55 }
 56 
 57 // dfs, hard to implement
 58 public class Solution1 {
 59     public string DecodeString(string s)
 60     {
 61         if (s == null || s.Length == 0) return "";
 62 
 63         return ParseString(s, 0, s.Length);
 64     }
 65 
 66     private string ParseString(string s, int start, int end)
 67     {
 68         if (start >= end) return "";
 69 
 70         int i = start;
 71         while (i < end && !Char.IsDigit(s[i]))
 72         {
 73             i++;
 74         }
 75 
 76         string pre = s.Substring(start, i - start);
 77 
 78         int count = 0;
 79         while (i < end && Char.IsDigit(s[i]))
 80         {
 81             count = count * 10 + (int)s[i] - (int)0;
 82             i++;
 83         }
 84 
 85         int strStart = ++i;
 86         int notClosed = 1, max = 1;
 87         var str = "";
 88 
 89         while (i < end)
 90         {
 91             if (s[i] == [)
 92             {
 93                 notClosed++;
 94             }
 95             else if (s[i] == ])
 96             {
 97                 notClosed--;
 98             }
 99 
100             max = Math.Max(max, notClosed);
101 
102             if (notClosed == 0)
103             {
104                 if (max == 1)
105                 {
106                     str = s.Substring(strStart, i - strStart);
107                 }
108                 else
109                 {
110                     str = ParseString(s, strStart, i);
111                 }
112 
113                 break;
114             }
115 
116             i++;
117         }
118 
119         var sb = new StringBuilder();
120 
121         for (int k = 0; k < count; k++)
122         {
123             sb.Append(str);
124         }
125 
126         return pre + sb.ToString() + ParseString(s, i + 1, end);
127     }
128 }

 



以上是关于Leetcode 394: Decode String的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-394. Decode String(DFS)

Python 解LeetCode:394 Decode String

Leetcode-394

LeetCode 394. 字符串解码

LeetCode 394. 字符串解码

394. Decode String