Back to Practice
#0264

Decode String

MediumDSA28 min20 XP

Problem

Decode strings where k[encoded] means encoded is repeated k times. Inputs are valid and may be nested.

Why This Matters

Decode String is a stack parsing problem that teaches how to preserve previous context before entering brackets.

Function Signature

def decode_string(s):

Examples

Example 1
Inputs = "3[a2[c]]"
Output"accaccacc"

a2[c] becomes acc, then it repeats three times.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Nested repeat
Input"3[a2[c]]"
Expected"accaccacc"

Decode the inner bracket before the outer bracket.

Multi-digit repeat
Input"10[a]"
Expected"aaaaaaaaaa"

The repeat count can have more than one digit.

Hidden Test Categories
multiple bracket groupsplain letters outside bracketsdeeply nested groups