# leetcode\_394

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

```
Examples:

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

## Solutions

1. **stack**
2. Reference: <https://leetcode-cn.com/problems/decode-string/solution/decode-string-fu-zhu-zhan-fa-di-gui-fa-by-jyd/>

```cpp
class Solution {
public:
    string decodeString(string s) {
        stack<pair<string, int>> lasts;
        string cur_str = "";
        int num = 0;
        for (auto & c : s) {
            // number
            if (c < 'A')
                num = num * 10 + (c - '0');
            // characters
            else if (c > ']' || c < '[')
                cur_str.push_back(c);
            else if (c == '[') {
                lasts.push(make_pair(move(cur_str), num));
                num = 0;
                cur_str = "";
            }
            else {
                auto left = lasts.top(); lasts.pop();
                string & last_str = left.first;
                for (int i = 0; i < left.second; i++)
                    last_str += cur_str;
                cur_str = last_str;
                num = 0;
            }
        }
        return cur_str;
    }
};
```

1. **recursion**

```cpp
class Solution {
private:
    int end = 0;
public:
    string decode(string & s, int cur) {
        int num = 0;
        string cur_str = "";
        while (cur < s.size()) {
            if (s[cur] >= '0' && s[cur] <= '9')
                num = num * 10 + (s[cur] - '0');
            else if (s[cur] == '[') {
                string bracket_str = decode(s, cur + 1);
                while (num--)
                    cur_str += bracket_str;
                cur = this->end;
                num = 0;
            }
            else if (s[cur] == ']') {
                this->end = cur;
                break;
            }
            else
                cur_str.push_back(s[cur]);
            cur++;
        }
        return cur_str;
    }

    string decodeString(string s) {
        return decode(s, 0);  
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zhongquan789.gitbook.io/leetcode/leetcode/leetcode_394.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
