Nested String Decompression
GoogleOnsiteMedium
A compressed string is built from lowercase letters and groups written (text){k}. A group stands for text repeated k times, and text may itself contain more groups.
Part 1: Decompress the string
You are given a compressed string s made of lowercase letters and groups written (text){k}. A group stands for text repeated k times, and text may itself contain more groups. Return the decompressed string.
s is well formed: every ( is closed by a ) that is followed by {k}, each group holds at least one character, and every other character is a lowercase letter. An empty s decompresses to an empty string.
- Example 1
- s
"a(abc){3}"- Output
"aabcabcabc"
Why: The group
(abc)repeats three times after the leadinga. - Example 2
- s
"a(b(c){2}){3}d"- Output
"abccbccbccd"
Why: The inner group
(c){2}makescc, so the outer group expands tobccrepeated three times betweenaandd.
Constraints
0 <= len(s) <= 10^42 <= k <= 99for every group- the decompressed string has at most
10^5characters - each
(is closed by a)followed by{k} - each group's
textis non-empty - every character of
soutside(,),{,}and the digits ofkis a lowercase letter