File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Problem Link: https://leetcode.com/problems/decode-string/
3
+
4
+ Given an encoded string, return it's decoded string.
5
+ The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly
6
+ k times. Note that k is guaranteed to be a positive integer.
7
+ You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
8
+ Furthermore, you may assume that the original data does not contain any digits and that digits are only for those
9
+ repeat numbers, k. For example, there won't be input like 3a or 2[4].
10
+
11
+ Examples:
12
+ s = "3[a]2[bc]", return "aaabcbc".
13
+ s = "3[a2[c]]", return "accaccacc".
14
+ s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
15
+ """
16
+ class Solution :
17
+ def decodeString (self , s ):
18
+ """
19
+ :type s: str
20
+ :rtype: str
21
+ """
22
+ stack = []
23
+ num = 0
24
+ curString = ''
25
+ for c in s :
26
+ if c == '[' :
27
+ stack .append (curString )
28
+ stack .append (num )
29
+ curString = ''
30
+ num = 0
31
+ elif c == ']' :
32
+ curNum = stack .pop ()
33
+ prevStr = stack .pop ()
34
+ curString = prevStr + curNum * curString
35
+ elif c .isnumeric ():
36
+ num = num * 10 + int (c )
37
+ else :
38
+ curString += c
39
+ return curString
You can’t perform that action at this time.
0 commit comments