2Brute2Best
🔥0

Decode String

Medium·LeetCode #394 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given an encoded string s. The rule is k[text]: the part inside the square brackets is written out k times in a row. k is a positive whole number and can have more than one digit.

Groups can be nested inside each other, like 3[a2[c]]. Return the fully decoded string.

You can assume the input is always well formed: brackets match, there are no extra spaces, digits only appear as repeat counts (never as text), and every [ comes right after its count.

Input

An encoded string s of lowercase letters, digits and square brackets.

Output

The decoded string.

Example 1
Input: s = "3[a]2[bc]"
Output: "aaabcbc"

"a" three times, then "bc" two times.

Example 2
Input: s = "3[a2[c]]"
Output: "accaccacc"

The inner 2[c] becomes "cc", so the group is "acc". Writing it 3 times gives "accaccacc".

Example 3
Input: s = "2[abc]3[cd]ef"
Output: "abcabccdcdcdef"

"abc" twice, "cd" three times, then the plain letters "ef".

Constraints
  • • 1 ≤ s.length ≤ 30
  • • s has lowercase letters, digits and '[' ']'
  • • Every repeat count k is in [1, 300]
  • • The decoded string is at most 10^5 characters long
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“k[encoded_string], can be nested”
→ Brackets that nest → the newest open group closes first → stack.
“repeated exactly k times”
→ Each `[` carries data (the count) that is only used at its `]` → push it.
“letters before a group”
→ The outer text must survive while the inner group is being built → push the text too.
03

Approach Complexity Ladder

Brute Force — expand the innermost group, repeatO(P · L)O(L)

Find the first ']', expand its k[...] in place, rebuild the string, repeat

Optimal — two stacks (counts and saved text)O(L · D)O(L)

On '[' push (k, cur) and reset; on ']' pop and cur = prev + cur × k

Brute Force Intuition

Find the first ]. The last [ before it opens a group with only letters inside, so it can be expanded right now: read the count before the [, write the inside k times, and rebuild the string with that group replaced. Repeat until no brackets are left.

Recognition rule for this sub-pattern:

Opening things wait on the stack until their closing partner arrives.
Remember this · Stack
Trigger
Matching pairs, nested structure, undo, evaluating expressions, "the last one wins".
Think
Does each new element need to look at the most recent unresolved element?
Key question
What does an item on the stack represent, and when does it get resolved?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Stack translate into production architectures and low-level memory mechanics.