Implement a parser that decodes a specially formatted string and returns the decoded result.
The input uses two kinds of group. A repetition k[encoded_string] stands for the decoded encoded_string repeated exactly k times. An annotation {key:value} becomes key=value in the output. Groups can be nested (for example 2[a3[b]c]) and concatenated (for example 2[ab]3[cd]), annotations can appear inside repetitions, and repetitions can appear inside annotation values. Plain letters outside any group are copied to the output unchanged.
Function Signature
def parse_string(s: str) -> str:
Rules
-
In
k[encoded_string]
,
k
is a positive integer written in decimal and may have more than one digit. Digits appear only as repeat counts, and every count is immediately followed by
[
.
-
{key:value}
is replaced by the key, then
=
, then the decoded value. The braces and the colon do not appear in the output.
-
The key is one or more lowercase letters. The
:
right after it is the only colon that belongs to that annotation. The value is any valid encoded string, so it can hold letters, repetitions and further annotations.
-
The input is always valid: there are no spaces, and every
[
and
{
has a matching
]
or
}
.
Constraints
-
1 <= s.length <= 10^4
-
s
consists of lowercase English letters, digits,
[
,
]
,
{
,
}
and
:
.
-
Every repeat count is in the range 1 to 300.
-
Nesting depth, counting both kinds of group, is at most 10.
-
The decoded string has at most 10^5 characters.
Examples
Input: s = "2[a3[b]c]"
Output: "abbbcabbbc"
The inner group 3[b] becomes bbb, so the outer group holds abbbc, and repeating it twice gives abbbcabbbc.
Input: s = "hello{name:world}2[z]"
Output: "helloname=worldzz"
The letters hello are copied, the annotation becomes name=world, and 2[z] becomes zz.
Input: s = "2[{k:v}]{a:3[b]{c:d}}"
Output: "k=vk=va=bbbc=d"
The first group repeats the annotation k=v twice. In the second annotation the value 3[b]{c:d} decodes to bbbc=d before it is attached to the key a.