# LeetCode_String_394. String Decode String [String, Recursive] [java] [Medium]

Catalog

1. Title Description

English Description

Chinese Description

Examples and instructions

2. Solving problems

Three, AC code

Java

4. Problem solving process

First Bo

Second Stroke

# 1. Title Description

## English Description

Given an encoded string, return its 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].

## Chinese Description

Given an encoded string, returns its decoded string.

The encoding rule is: k[encoded_string], which means encoded_within square brackets String repeats exactly k times. Note that K is guaranteed to be a positive integer.

You can think that the input string is always valid; There are no extra spaces in the input string, and the square brackets you enter always conform to the formatting requirements.

In addition, you can assume that the original data does not contain numbers, and that all numbers represent only the number of repetitions k, for example, there will be no such occurrence   3a   or   2[4]   Input.

Source: LeetCode

# 2. Solving problems

Core issue: Find matching left and right parentheses (find the right parentheses that match the leftmost parentheses, you can recursively call the algorithm on the middle string).

• Since brackets may be side-by-side ([][]]]) or nested ([[]]]), the number of left brackets level needs to be recorded. If the character is'[', level++, if']', level-- (which means offsetting a left parenthesis);
• When level is 0, matching left and right parentheses are found.

The main problem has been solved, so let's debug the details slowly()

# Three, AC code

## Java

class Solution {
public String decodeString(String s) {
StringBuilder repeat = new StringBuilder();     // Number of times storage needs to be repeated
StringBuilder content = new StringBuilder();    // Store raw data (excluding numbers)
StringBuilder ans = new StringBuilder();        // Store results
int start = -1, level = 0;                      // start: The position of the first left parenthesis, level: Number of left parentheses
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
// When the number of left parentheses is 0
if (level == 0) {
if (c == '[') {
if (level == 0) {
start = i;                      // Record the position of the first left parenthesis
}
level++;// Number of left parentheses plus one
} else if (c >= '0' && c <= '9') {
// When a character precedes a number, add the data stored in the context to the result
if (content.length() != 0) {
ans.append(content.toString());
content.setLength(0);
}
repeat.append(c);
} else {
content.append(c);
}
}
// When the number of left parentheses is not zero
else {
if (c == '[') {
level++;                            // Number of left parentheses plus one
} else if (c == ']') {
level--;                            // Number of left parentheses minus one
// Find the position of the right parenthesis that matches the first left parenthesis
if (level == 0) {
// Recursively get the result of string processing within left and right parentheses
String s1 = decodeString(s.substring(start + 1, i));
int num = Integer.valueOf(repeat.toString());
for (int j = 0; j < num; j++) {
ans.append(s1);
}
repeat.setLength(0);            // Set the previously recorded duplicate number to 0
}
}
}
}
// Handle cases where there are no parentheses in the string, such as "abc"
if (content.length() != 0) {
ans.append(content.toString());
}
return ans.toString();
}
}

# 4. Problem solving process

## First Bo

I thought it was easy at first. The result is still unclear about the recursive process. I can't write half of it and find the algorithm is totally wrong

class Solution {
public String decodeString(String s) {
int start = -1, end = -1;
StringBuilder repeat = new StringBuilder();
StringBuilder content = new StringBuilder();
StringBuilder ans = new StringBuilder();

for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '[') {
start = i;
continue;
} else if (c == ']') {
end = i;
String s1 = decodeString(s.substring(start + 1, end));
start = -1;
end = -1;
System.out.println(repeat.toString());
int num = Integer.valueOf(repeat.toString());
for (int j = 0; j < num; j++) {
ans.append(s1);
}
repeat.setLength(0);
content.setLength(0);
} else if (c >= '0' && c <= '9') {
if (content.length() != 0) {
ans.append(content.toString());
content.setLength(0);
}
repeat.append(c);
} else {
content.append(c);
}
}
ans.append(content.toString());
return ans.toString();
}
}