DIY: Decoding a String

Solve the interview question "Decoding a String" in this lesson.

Problem statement

Given an encoded string, return its decoded string.

The input string is encoded such that k[pattern] should be decoded to pattern repeatedly for k times. Note that pattern itself may include nested repetition encoding. For example, 3[a2[b]] represents abbabbabb.

k is guaranteed to be a positive integer. Furthermore, the string does not contain any digits, and any digit in the encodedString only represents k.


The input will be in the form of a string. The following is an example input:

s = "abc3[cd]xyz"


The output will also be in the form of a string, and it will be the decoded version of the input string. The following is an example output for the above list:

Output: "abccdcdcdxyz"

Coding exercise

Implement the decodeString(s) function, where s is the encoded string that you have to decode.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.