This question was asked in UBER technical Interview 2021

950
Aug 28, 2021

Given an encoded string, you have to output the decoded string. The encoded string will have the following format: 1[ac2[de]], 2[a2[b3]], 3[a3[b23c1[d]k2[x]]]. Basically, a string consists of alphabets, numbers, and square brackets. Before each square bracket, there will be a sequence of numbers. The sequence of numbers represents how many times they have to be repeated in the resultant string. See example for better understanding

Input: 1[ac2[de]]
Output: acdede
Explanation: 2[de] means "de" should be
repeated two times and 1[ac....] means
whatever was the resultant string between
the square brackets should be repeated only once.

Input: 2[a4[b]c13d2[e]]
Output: abbbbc13deeabbbbc13dee
Explanation: As you can see, the decoded
string may also contain digits because those digits
are not followed by a square bracket in the encoded string.

Input: 1[a2[b1[c11[d]]]]
Output: abcdddddddddddbcddddddddddd
Explaination: This example shows that there
can be multiple digits before a square bracket(11 in
this case)

Comments (3)