Decode a string recursively encoded as count followed by substring | Set 2 (using Recursion)
An encoded string str is given. The pattern in which the string is encoded is as follows.
<count>[sub_str] ==> The substring ‘sub_str’ appears count times.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
The task is to decode this string str.
Examples:
Input: str = “1[b]”
Output: b
Explanation: The substring ‘b’ is repeated 1 time.Input: str = “2[ab]”
Output: abab
Explanation: The substring “ab” is repeated two times.Input: str = “2[a2[b]]”
Output: abbabb
Explanation: The substring ‘b’ is repeated 2 times and added to ‘a’ which forms a substring “abb”.
Now this substring is repeated two time. So the final string is “abbabb”.
Iterative Approach: The iterative approach is mentioned in the Set-1 of this problem.
Recursive Approach: In this article, the problem will be solved using Recursion and Stack. Follow the approach mentioned below to solve the problem
- Declare a stack
- Recursively traverse each character of the given string one by one. There can be 4 cases:
- Case 1: Current character is ‘[‘
- No need to do anything in this case. Just continue to next character
- Case 2: Current character is ‘]’
- Pop the top string temp and top integer x from the stack
- Repeat the string temp, x times
- If the next top element in the stack is a string, append this repeated string to the top string
- else push the repeated string into the stack
- Case 3: Current character is a digit
- If previous char of original string was also a digit, append this digit to the number on top of the stack
- If previous char was something else, push this digit into the stack
- Case 4: Current character is an alphabet
- If previous char of original string was also an alphabet, append this alphabet to the string on top of the stack
- If previous char was something else, push this alphabet into the stack
- Case 1: Current character is ‘[‘
- At the end, return the string in the stack and print it
Below is the implementation of the above approach.
C++
// C++ code to implement above approach#include <bits/stdc++.h>using namespace std; // Stack to store intermediate stringsstack<string> ans;string res = ""; // Recusrsive function to decode // the encoded stringvoid decode(string s, int i){ if (i == s.length()) { res = ans.top(); return; } // If the string character is '[' if (s[i] == '['); // If the string character is ']' else if (s[i] == ']') { string temp = ans.top(); ans.pop(); int x = stoi(ans.top()); ans.pop(); for (string j = temp; x > 1; x--) temp = temp + j; string temp1 = ans.empty() == false ? ans.top() : ""; if (!temp1.empty() && !(temp1[0] - '0' < 10)) { ans.pop(); temp1 = temp1 + temp; ans.push(temp1); } else { ans.push(temp); } } // If string character is a digit else if (s[i] - '0' < 10) { string temp = ans.empty() == false ? ans.top() : ""; if (!temp.empty() && temp[0] - '0' < 10 && s[i - 1] - '0' < 10) { ans.pop(); temp = temp + s[i]; ans.push(temp); } else { temp = s[i]; ans.push(temp); } } // If the string character is alphabet else if (s[i] - 'a' < 26) { string temp = ans.empty() == false ? ans.top() : ""; if (!temp.empty() && temp[0] - 'a' >= 0 && temp[0] - 'a' < 26) { ans.pop(); temp = temp + s[i]; ans.push(temp); } else { temp = s[i]; ans.push(temp); } } // Recursive call for next index decode(s, i + 1);} // Function to call the recursive functionstring decodeString(string s){ decode(s, 0); return res;} // Driver codeint main(){ string str = "2[a2[b]]"; cout << decodeString(str) << endl; return 0;} |
abbabb
Time Complexity: O(N) where N is the length of the decoded string
Auxiliary Space: O(N)

