Decode a string with nested repeat counts and key:value annotations

Quick Overview

Decode a string where k[text] repeats its contents k times and each {key:value} annotation becomes key=value, with both kinds of group nested inside each other in either direction. It tests stack-based parsing of nested structures, multi-digit repeat counts and building the output without quadratic copying.

Decode a string with nested repeat counts and key:value annotations

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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 ```python 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 ```text 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`. ```text 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`. ```text 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`.

Overview: Decode a string where k[text] repeats its contents k times and each {key:value} annotation becomes key=value, with both kinds of group nested inside each other in either direction. It tests stack-based parsing of nested structures, multi-digit repeat counts and building the output without quadratic copying.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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.