# Leetcode 394. String decoding | Python

Time：2021-4-20

### 394. String decoding

Title Source: leetcode https://leetcode-cn.com/problems/decode-string

#### subject

Given an encoded string, returns its decoded string.

The coding rule is: K [encoded]_ String], indicating the encoded inside the square brackets_ String is repeated exactly k times. Note that K is guaranteed to be a positive integer.

You can assume that the input string is always valid; There are no extra spaces in the input string, and the input brackets are always formatted.

In addition, you can think that the original data does not contain numbers, all the numbers only represent the number of repetitions K, for example, there will not be such as 3A or 2 [ four ] Input of.

Example:

``````S = "3 [a] 2 [BC]", return "aaabcbc"
S = 3 [A2 [C]], returns "acccacc"
S = "2 [ABC] 3 [CD] EF", return "abcabccdef"``````

#### Thinking of solving problems

Idea: stack

In the above example, the second example shows the case of parentheses. In this problem, the more difficult part is also this part.

In this space, we use the idea of stack to solve this problem. In the case of bracket embedding mentioned above, when we return, we need to generate and splice characters from the inside out. This is also the reason why we use the stack, because the stack has the feature of first in, then out.

Here, in our stack, each item needs to store two pieces of information, one is the character before the left bracket, and the other is the number before the left bracket.

For example, example 2,`3[a2[c]]`Of which`a2[c]`This part should be like this:`('a', 2)`After that, the character c will be traversed. When the right bracket is encountered, the stack operation will be performed. Pop up elements`('a', 2)`Here, it is spliced with the character c to call the new string`a + 2 * 'c'`That is to say`acc`

The specific methods are as follows:

• When the traversal element is a number, the numeric character is converted to a number, which is used for the multiple operation of the subsequent character decoding;
• When the traversal element is a letter, the character is directly added at the end of the result;
• When the traversal element is left bracket, the letters and numbers in front of the left bracket should be put on the stack. After putting on the stack, the variables storing the two items should be reset;
• When the traversal element is the right bracket, the stack is pulled out and the characters are spliced.

The implementation process of the algorithm is illustrated as follows:

The specific code implementation is as follows.

#### code implementation

``````class Solution:
def decodeString(self, s: str) -> str:
#Auxiliary stack, each item stores two information, a character before the left bracket and a number before the left bracket
stack = []
mult = 0
ans = ''

for char in s:
#When the character is a numeric character, it is converted to a numeric value for subsequent multiple operation
if char.isdigit():
# mult = int(char)
#Note the number of digits,
#In the above writing, there is an error in the use case "100 [leetcode]", which is changed to
mult = mult * 10 + int(char)
#When the left bracket is encountered, the preceding characters and values are put on the stack
elif char == '[':
stack.append((ans, mult))
#Here you reset the variables that store these two items
ans = ''
mult = 0
#When the right bracket is encountered, the stack will exit
elif char == ']':
front_char, cur_mult = stack.pop()
ans = front_char + cur_mult * ans
#This means that the character encountered is directly added to the end of ANS
else:
ans += char

return ans``````

#### summary

• This problem is more difficult to deal with is the problem of brackets embedded, here to expand from the inside out, here in line with the characteristics of the stack first in and then out, so we use the idea of stack to solve this problem.
• Build auxiliary stack, each item stores two information, a character before the left bracket, and a value before the left bracket
• Traverse the string and deal with different situations

• When the traversal element is a numeric character, it is converted to a numeric value for subsequent multiple calculation (note that the number of digits is not necessarily a single digit)
• When the traversal element is a letter, it is added to the end of the result
• When the traversal element is the left bracket, the stack operation is carried out. The characters and numbers in front of the left bracket are pressed into the stack. At the same time, the variables storing the two items are reset
• When the traversal element is the right bracket, the stack is started and the characters are spliced at the same time.

Welcome to WeChat official account, book collection.

## Analysis of super comprehensive MySQL statement locking (Part 1)

A series of articles： Analysis of super comprehensive MySQL statement locking (Part 1) Analysis of super comprehensive MySQL statement locking (Part 2) Analysis of super comprehensive MySQL statement locking (Part 2) Preparation in advance Build a system to store heroes of the Three KingdomsheroTable: CREATE TABLE hero ( number INT, name VARCHAR(100), country varchar(100), PRIMARY […]