An encoded string (s) is given, the task is to decode it. The pattern in which the strings are encoded is as follows.
<count>[sub_str] ==> The substring 'sub_str' appears count times.
Input : str = "1[b]" Output : b Input : str = "2[ab]" Output : abab Input : str = "2[a2[b]]" Output : abbabb Input : str = "3[b2[ca]]" Output : bcacabcacabcaca
The idea is to use two stacks, one for integers and another for characters.
Now, traverse the string,
- Whenever we encounter any number, push it into the integer stack and in case of any alphabet (a to z) or open bracket (‘[‘), push it onto the character stack.
- Whenever any close bracket (‘]’) is encounter pop the character from the character stack until open bracket (‘[‘) is not found in the character 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.
Below is implementation of this approach:
<!—-Illustration of above code for “3[b2[ca]]”>
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Decode the string encoded with the given algorithm
- Decode an Encoded Base 64 String to ASCII String
- Count occurrences of a substring recursively
- Maximize count of 0s in left and 1s in right substring by splitting given Binary string
- Maximum number of set bits count in a K-size substring of a Binary String
- Decode a median string to the original string
- Check if a string can become empty by recursively deleting a given sub-string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Find length of longest subsequence of one string which is substring of another string
- Minimum changes required to make first string substring of second string
- Queries to check if string B exists as substring in string A
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Check if the given string is shuffled substring of another string
- Longest substring with count of 1s more than 0s
- Check if a string is substring of another
- Frequency of a substring in a string
- Count of distinct characters in a substring by given range for Q queries
- First substring whose reverse is a word in the string