Decode String

Given an encoded string A consisting of lowercase English alphabets, square parentheses, and digits.
The encoding rule is X[encoded string],
where the encoded_string inside the square brackets is being repeated exactly X times.
Note that X is guaranteed to be a positive integer.

Find and return the decoded string.


  1. You may assume that the original data does not contain any digits and that digits are only for those repeat numbers,
    X. For example, there won’t be input like 3a or 2[4].
  2. You may assume that the input string is always valid. No extra white spaces, square brackets are well-formed, etc.
  3. You may assume that the length of the decoded string will not exced 200000.

Input Format

The only argument given is string A.

Output Format

Return the decoded string.


1 <= length of the string <= 100000

For Example

Input 1:
    A = "3[a]2[bc]"
Output 1:

Input 2:
    A = "3[a2[c]]"
Output 2:
Input 3:
    A = "2[abc]3[cd]ef"
Output 2:
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Decode String on Interview Code Editor
Sign Up
to access hints and editorial solutions for Decode String


Click here to start solving coding interview questions