VOOZH about

URL: https://www.geeksforgeeks.org/dsa/decode-string-recursively-encoded-count-followed-substring/

⇱ Decode String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Decode String

Last Updated : 5 Sep, 2025

Given an encoded string s, decode it by expanding the pattern k[substring], where the substring inside brackets is written k times. Return the final decoded string.

Examples:

Input: s = "3[b2[ca]]"
Output: bcacabcacabcaca
Explanation:
Inner substring "2[ca]" breakdown into "caca".
Now , new string becomes "3[bcaca]"
Similarly "3[bcaca]" becomes "bcacabcacabcaca" which is final result.

Input: s = "3[ab]"
Output: ababab
Explanation: The substring "ab" is repeated 3 times, giving "ababab".

[Approach 1] Using Two Stacks - O(n) Time and O(n) Space

Use two stacks (one for numbers, one for characters). When ']' is found, pop till '[' to form a substring, repeat it using the top number, and push back β€” final stack gives the decoded string.

Steps of implementation:

  1. Initialize two stacks β†’ one for numbers and one for characters.
  2. Traverse the string character by character.
    => If it’s a number β†’ push it to the number stack.
    => If it’s an alphabet (a–z) or '[' β†’ push it to the character stack.
    => If it’s a closing bracket ']':
  3. Whenever any close bracket (']') is encountered, pop the characters from the character stack until open bracket ('[') is found in the stack. Also, pop the top element from the integer stack, say n. Now make a string repeating the popped character n number of time. Now, push all character of the string in the stack.
  4. After traversing whole string, integer stack will be empty and last string which will be formed will be the given result.

Illustration:



Output
bcacabcacabcaca

[Approach 2] Using Single Stack - O(n) Time and O(n) Space

In this approach, we use a single stack to store both characters and digits. Instead of maintaining a separate integer stack for storing repetition counts, we store the digits directly in the main stack. The key observation is that the number always appears before the opening bracket '['. This allows us to retrieve it later without needing an extra stack.

Steps of implementation:

  • Initialize an empty stack.
  • Push characters onto the stack until ']' is encountered.
  • When ']' is found:
    => Pop characters to form the substring until '[' is found, then remove '['.
    => Extract the preceding number from the stack and convert it to an integer.
    => Repeat the substring and push the expanded result back onto the stack.
  • After traversal, pop all characters from the stack, reverse them, and return the final decoded string.

Output
bcacabcacabcaca

[Alternate Approach] Without Using Stack - O(n) Time and O(n) Space

The approach is like traversing the encoded string character by character while maintaining a result string. Whenever a closing bracket ']' is found, we extract the substring enclosed within the matching opening bracket '[' and retrieve the number that indicates how many times the substring should be repeated. This repeated substring is then added back to the current result. By continuing this process until the end of the string, we obtain the fully decoded output.


Output
bcacabcacabcaca
Comment
Article Tags: