Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Decode String
EASY
15 mins
24 upvotes
Strings
Stacks & Queues
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Strings
-
-
Stacks & Queues
-
-
Other topics
Problem solved
Badge
Skill meter
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Decode String

Contributed by
Anish De
Easy
yellow-spark
0/40
Avg time to solve 15 mins
Success Rate 85 %
Share
24 upvotes

Problem Statement

You have been given an encoded string. Your task is to decode it back to the original string.

- An encoded string will be of the form <count>[encoded_string], where the 'encoded_string' inside the square brackets is being repeated exactly 'count' times. Note that 'count' is guaranteed to be a positive integer and can be greater than 9.
- There are no extra white spaces and square brackets are well-formed.
For example -
Input: 2[a]
“a” is encoded 2 times, hence the decoded string will be "aa".

Input: 3[a2[b]]
“b” is encoded 2 times, which decodes as 3[abb]. Now, "abb" is encoded 3 times, hence decoded string will be "abbabbabb". 
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
3[a]2[bc]
a1[b]1[cc]
Sample Output 1:
aaabcbc
abcc
Explanation for sample 1:
For the first test case, "a" is encoded 3 times and "bc" is encoded 2 times. After combining these two strings after decoding, we'll get "aaa" + "bcbc" = "aaabcbc".
Sample Input 2:
1
ab2[c3[b]]
Sample Output 2:
abcbbbcbbb
Reset Code
Full screen
Auto
copy-code
Console