Given an encoded string s, decode it by expanding the pattern k[substring], where the substring inside brackets is written k times. Return the final decoded string.
Examples:
Input: s = "3[b2[ca]]"
Output: bcacabcacabcaca
Explanation:
Inner substring "2[ca]" breakdown into "caca".
Now , new string becomes "3[bcaca]"
Similarly "3[bcaca]" becomes "bcacabcacabcaca" which is final result.
Input: s = "3[ab]"
Output: ababab
Explanation: The substring "ab" is repeated 3 times, giving "ababab".
[Approach 1] Using Two Stacks - O(n) Time and O(n) Space
Use two stacks (one for numbers, one for characters). When ']' is found, pop till '[' to form a substring, repeat it using the top number, and push back — final stack gives the decoded string.
Steps of implementation:
- Initialize two stacks → one for numbers and one for characters.
- Traverse the string character by character.
=> If it’s a number → push it to the number stack.
=> If it’s an alphabet (a–z) or '[' → push it to the character stack.
=> If it’s a closing bracket ']': - Whenever any close bracket (']') is encountered, pop the characters from the character stack until open bracket ('[') is found in the 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.
- After traversing whole string, integer stack will be empty and last string which will be formed will be the given result.
Illustration:
C++
#include <iostream>
#include <stack>
#include <string>
using namespace std;
string decodedString(string &s) {
stack<int> numStack;
stack<char> charStack;
string temp = "";
string res = "";
for (int i = 0; i < s.length(); i++) {
int cnt = 0;
// If Digit, convert it into number and
// push it into integerstack.
if (s[i] >= '0' && s[i] <= '9') {
while (s[i] >= '0' && s[i] <= '9') {
cnt = cnt * 10 + s[i] - '0';
i++;
}
i--;
numStack.push(cnt);
}
// If closing bracket ']' is encountered
else if (s[i] == ']') {
temp = "";
cnt = numStack.top();
numStack.pop();
// pop element till opening bracket '[' is not found in the
// character stack.
while (charStack.top() != '[') {
temp = charStack.top() + temp;
charStack.pop();
}
charStack.pop();
// Repeating the popped string 'temp' count number of times.
for (int j = 0; j < cnt; j++)
res = res.append(temp);
// Push it in the character stack.
for (int j = 0; j < res.length(); j++)
charStack.push(res[j]);
res = "";
}
else
charStack.push(s[i]);
}
// Pop all the element, make a string and return.
while (!charStack.empty()) {
res = charStack.top() + res;
charStack.pop();
}
return res;
}
int main() {
string s = "3[b2[ca]]";
cout << decodedString(s) << endl;
return 0;
}
Java
import java.util.Stack;
class GfG {
static String decodedString(String s) {
Stack<Integer> numStack = new Stack<>();
Stack<Character> charStack = new Stack<>();
String temp = "";
StringBuilder res = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
int cnt = 0;
// If Digit, convert it into a number and
// push it into the integer stack.
if (Character.isDigit(s.charAt(i))) {
while (Character.isDigit(s.charAt(i))) {
cnt = cnt * 10 + (s.charAt(i) - '0');
i++;
}
i--;
numStack.push(cnt);
}
// If closing bracket ']' is encountered
else if (s.charAt(i) == ']') {
temp = "";
cnt = numStack.pop();
// Pop elements till the opening bracket '[' is found
// in the character stack.
while (charStack.peek() != '[') {
temp = charStack.pop() + temp;
}
charStack.pop();
// Repeating the popped string 'temp' count number of times.
StringBuilder repeated = new StringBuilder();
for (int j = 0; j < cnt; j++) {
repeated.append(temp);
}
// Push it into the character stack.
for (int j = 0; j < repeated.length(); j++) {
charStack.push(repeated.charAt(j));
}
} else {
charStack.push(s.charAt(i));
}
}
// Pop all the elements, make a string, and return.
while (!charStack.isEmpty()) {
res.insert(0, charStack.pop());
}
return res.toString();
}
public static void main(String[] args) {
String s = "3[b2[ca]]";
System.out.println(decodedString(s));
}
}
Python
def decodedString(s):
numStack = []
charStack = []
temp = ""
res = ""
i = 0
while i < len(s):
cnt = 0
# If Digit, convert it into number and
# push it into integer stack.
if s[i].isdigit():
while i < len(s) and s[i].isdigit():
cnt = cnt * 10 + int(s[i])
i += 1
i -= 1
numStack.append(cnt)
# If closing bracket ']' is encountered
elif s[i] == ']':
temp = ""
cnt = numStack.pop()
# Pop element till opening bracket '[' is not found
# in the character stack.
while charStack[-1] != '[':
temp = charStack.pop() + temp
charStack.pop()
# Repeating the popped string 'temp' count number of times.
res = temp * cnt
# Push it in the character stack.
for c in res:
charStack.append(c)
res = ""
else:
charStack.append(s[i])
i += 1
# Pop all the elements, make a string and return.
while charStack:
res = charStack.pop() + res
return res
if __name__ == "__main__":
s = "3[b2[ca]]"
print(decodedString(s))
C#
using System;
using System.Collections.Generic;
using System.Text;
class GfG {
static string decodedString(string s) {
Stack<int> numStack = new Stack<int>();
Stack<char> charStack = new Stack<char>();
string temp = "";
StringBuilder res = new StringBuilder();
for (int i = 0; i < s.Length; i++) {
int cnt = 0;
// If Digit, convert it into a number and
// push it into the integer stack.
if (char.IsDigit(s[i])) {
while (char.IsDigit(s[i])) {
cnt = cnt * 10 + (s[i] - '0');
i++;
}
i--;
numStack.Push(cnt);
}
// If closing bracket ']' is encountered
else if (s[i] == ']') {
temp = "";
cnt = numStack.Pop();
// Pop elements till the opening bracket '[' is found
// in the character stack.
while (charStack.Peek() != '[') {
temp = charStack.Pop() + temp;
}
charStack.Pop();
// Repeating the popped string 'temp' count number of times.
StringBuilder repeated = new StringBuilder();
for (int j = 0; j < cnt; j++) {
repeated.Append(temp);
}
// Push it into the character stack.
foreach (char c in repeated.ToString()) {
charStack.Push(c);
}
} else {
charStack.Push(s[i]);
}
}
// Pop all the elements, make a string, and return.
while (charStack.Count > 0) {
res.Insert(0, charStack.Pop());
}
return res.ToString();
}
static void Main(string[] args) {
string s = "3[b2[ca]]";
Console.WriteLine(decodedString(s));
}
}
JavaScript
function decodedString(s) {
const numStack = [];
const charStack = [];
let temp = "";
let res = "";
for (let i = 0; i < s.length; i++) {
let cnt = 0;
// If Digit, convert it into number and
// push it into integer stack.
if (!isNaN(s[i])) {
while (!isNaN(s[i])) {
cnt = cnt * 10 + (s[i] - '0');
i++;
}
i--;
numStack.push(cnt);
}
// If closing bracket ']' is encountered
else if (s[i] === ']') {
temp = "";
cnt = numStack.pop();
// Pop element till opening bracket '[' is not found
// in the character stack.
while (charStack[charStack.length - 1] !== '[') {
temp = charStack.pop() + temp;
}
charStack.pop();
// Repeating the popped string 'temp' count number of times.
res = temp.repeat(cnt);
// Push it in the character stack.
for (const c of res) {
charStack.push(c);
}
res = "";
}
else {
charStack.push(s[i]);
}
}
// Pop all the elements, make a string and return.
while (charStack.length > 0) {
res = charStack.pop() + res;
}
return res;
}
// Driver Code
const s = "3[b2[ca]]";
console.log(decodedString(s));
[Approach 2] Using Single Stack - O(n) Time and O(n) Space
In this approach, we use a single stack to store both characters and digits. Instead of maintaining a separate integer stack for storing repetition counts, we store the digits directly in the main stack. The key observation is that the number always appears before the opening bracket '['. This allows us to retrieve it later without needing an extra stack.
Steps of implementation:
- Initialize an empty stack.
- Push characters onto the stack until
']' is encountered. - When
']' is found:
=> Pop characters to form the substring until '[' is found, then remove '['.
=> Extract the preceding number from the stack and convert it to an integer.
=> Repeat the substring and push the expanded result back onto the stack. - After traversal, pop all characters from the stack, reverse them, and return the final decoded string.
C++
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
string decodeString(string &s) {
stack<char> st;
// Traverse the input string
for (int i = 0; i < s.length(); i++) {
// Push characters into the stack until ']' is encountered
if (s[i] != ']') {
st.push(s[i]);
}
// Decode when ']' is found
else {
string temp;
// Pop characters until '[' is found
while (!st.empty() && st.top() != '[') {
temp.push_back(st.top());
st.pop();
}
// Reverse the string
reverse(temp.begin(), temp.end());
st.pop();
string num;
// Extract the number (repetition count) from the stack
while (!st.empty() && isdigit(st.top())) {
num = st.top() + num;
st.pop();
}
// Convert extracted number to integer
int number = stoi(num);
string repeat;
// Repeat the extracted string 'number' times
for (int j = 0; j < number; j++)
repeat.append(temp);
// Push the expanded string back onto the stack
for (char c : repeat)
st.push(c);
}
}
string res;
// Pop all characters from stack to form the final result
while (!st.empty()) {
res.push_back(st.top());
st.pop();
}
// Reverse to get the correct order
reverse(res.begin(), res.end());
return res;
}
int main() {
string str = "3[b2[ca]]";
cout << decodeString(str);
return 0;
}
Java
import java.util.Stack;
class GfG {
static String decodeString(String s) {
Stack<Character> st = new Stack<>();
for (int i = 0; i < s.length(); i++) {
// Push characters into the stack until ']' is encountered
if (s.charAt(i) != ']') {
st.push(s.charAt(i));
}
// Decode when ']' is found
else {
StringBuilder temp = new StringBuilder();
// Pop characters until '[' is found
while (!st.isEmpty() && st.peek() != '[') {
temp.append(st.pop());
}
//Reverse the string
temp.reverse();
st.pop();
StringBuilder num = new StringBuilder();
// Extract the number (repetition count) from the stack
while (!st.isEmpty() && Character.isDigit(st.peek())) {
num.insert(0, st.pop());
}
// Convert extracted number to integer
int number = Integer.parseInt(num.toString());
StringBuilder repeat = new StringBuilder();
// Repeat the extracted string 'number' times
for (int j = 0; j < number; j++)
repeat.append(temp);
// Push the expanded string back onto the stack
for (char c : repeat.toString().toCharArray())
st.push(c);
}
}
StringBuilder res = new StringBuilder();
// Pop all characters from stack to form the final result
while (!st.isEmpty()) {
res.append(st.pop());
}
// Reverse to get the correct order
res.reverse();
return res.toString();
}
public static void main(String[] args) {
String str = "3[b2[ca]]";
System.out.println(decodeString(str));
}
}
Python
def decodeString(s: str) -> str:
st = []
for i in range(len(s)):
# Push characters into the stack until ']' is encountered
if s[i] != ']':
st.append(s[i])
# Decode when ']' is found
else:
temp = []
# Pop characters until '[' is found
while st and st[-1] != '[':
temp.append(st.pop())
temp.reverse()
st.pop()
num = []
# Extract the number (repetition count) from the stack
while st and st[-1].isdigit():
num.insert(0, st.pop())
# Convert extracted number to integer
number = int("".join(num))
repeat = "".join(temp) * number
# Push the expanded string back onto the stack
st.extend(repeat)
# Pop all characters from stack to form the final result
return "".join(st)
if __name__ == "__main__":
str_val = "3[b2[ca]]"
print(decodeString(str_val))
C#
using System;
using System.Collections.Generic;
using System.Text;
class GfG {
static string decodeString(string s) {
Stack<char> st = new Stack<char>();
// Traverse the input string
for (int i = 0; i < s.Length; i++) {
// Push characters into the stack until ']' is encountered
if (s[i] != ']') {
st.Push(s[i]);
}
// Decode when ']' is found
else {
StringBuilder temp = new StringBuilder();
// Pop characters until '[' is found
while (st.Count > 0 && st.Peek() != '[') {
temp.Insert(0, st.Pop());
}
// Remove '[' from the stack
st.Pop();
StringBuilder num = new StringBuilder();
// Extract the number (repetition count) from the stack
while (st.Count > 0 && char.IsDigit(st.Peek())) {
num.Insert(0, st.Pop());
}
// Convert extracted number to integer
int number = int.Parse(num.ToString());
StringBuilder repeat = new StringBuilder();
// Repeat the extracted string 'number' times
for (int j = 0; j < number; j++)
repeat.Append(temp);
// Push the expanded string back onto the stack
foreach (char c in repeat.ToString())
st.Push(c);
}
}
StringBuilder res = new StringBuilder();
// Pop all characters from stack to form the final result
while (st.Count > 0) {
// Insert at beginning to maintain order
res.Insert(0, st.Pop());
}
return res.ToString();
}
static void Main(string[] args) {
string str = "3[b2[ca]]";
Console.WriteLine(decodeString(str));
}
}
JavaScript
function decodedString(s) {
let stack = [];
let temp = "";
let res = "";
for (let i = 0; i < s.length; i++) {
let cnt = 0;
// If number, convert it into number
if (s[i] >= '0' && s[i] <= '9') {
while (s[i] >= '0' && s[i] <= '9') {
cnt = cnt * 10 + (s[i] - '0');
i++;
}
i--;
// converting the integer into
// char in order to store in a stack.
stack.push(cnt.toString());
}
// If closing bracket ']', pop element until
// '[' opening bracket is not found in the
// stack.
else if (s[i] === ']') {
temp = "";
while (stack[stack.length - 1] !== '[') {
temp = stack.pop() + temp;
}
// Now top element of stack is '['.
// Let's pop it to get the integer
stack.pop();
// Top element of stack will give the integer in char form.
// converting into integer.
cnt = parseInt(stack.pop(), 10);
// Repeating the popped string 'temp' count number of times.
for (let j = 0; j < cnt; j++) {
res += temp;
}
// Push it in the character stack.
for (let j = 0; j < res.length; j++) {
stack.push(res[j]);
}
res = "";
}
else {
stack.push(s[i]);
}
}
// Pop all the element, make a string and return.
while (stack.length > 0) {
res = stack.pop() + res;
}
return res;
}
// Driver code
const s = "3[b2[ca]]";
console.log(decodedString(s));
[Alternate Approach] Without Using Stack - O(n) Time and O(n) Space
The approach is like traversing the encoded string character by character while maintaining a result string. Whenever a closing bracket ']' is found, we extract the substring enclosed within the matching opening bracket '[' and retrieve the number that indicates how many times the substring should be repeated. This repeated substring is then added back to the current result. By continuing this process until the end of the string, we obtain the fully decoded output.
C++
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string decodedString(string &s) {
string res = "";
for (int i = 0; i < s.length(); i++) {
// If the current character is not a clostring
// bracket, append it to the result string.
if (s[i] != ']') {
res.push_back(s[i]);
}
else {
string temp = "";
while (!res.empty() && res.back() != '[') {
temp.push_back(res.back());
res.pop_back();
}
// Reverse the temporary string to obtain the
// correct substring.
reverse(temp.begin(), temp.end());
// Remove the opening bracket from the result
// string.
res.pop_back();
// Extract the preceding number and convert it
// to an integer.
string num = "";
while (!res.empty() && res.back() >= '0' && res.back() <= '9') {
num.push_back(res.back());
res.pop_back();
}
reverse(num.begin(), num.end());
int p = stoi(num);
// Append the substring to the result string,
// repeat it to the required number of times.
while (p--) {
res.append(temp);
}
}
}
// Return the decoded string.
return res;
}
int main() {
string s = "3[b2[ca]]";
cout << decodedString(s);
return 0;
}
Java
class GfG {
static String decodedString(String s) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ']') {
res.append(s.charAt(i));
}
else {
StringBuilder temp = new StringBuilder();
while (res.length() > 0
&& res.charAt(res.length() - 1)
!= '[') {
temp.insert(
0, res.charAt(res.length() - 1));
res.deleteCharAt(res.length() - 1);
}
// Remove the opening bracket from the
// result string.
res.deleteCharAt(res.length() - 1);
// Extract the preceding number and convert
// it to an integer.
StringBuilder num = new StringBuilder();
while (res.length() > 0
&& Character.isDigit(
res.charAt(res.length() - 1))) {
num.insert(
0, res.charAt(res.length() - 1));
res.deleteCharAt(res.length() - 1);
}
int p = Integer.parseInt(num.toString());
// Append the substring to the result
// string, repeat it to the required number
// of times.
for (int j = 0; j < p; j++) {
res.append(temp.toString());
}
}
}
return res.toString();
}
public static void main(String[] args) {
String s = "3[b2[ca]]";
System.out.println(decodedString(s));
}
}
Python
def decodedString(s):
res = ""
for i in range(len(s)):
if s[i] != ']':
res += s[i]
else:
temp = ""
while res and res[-1] != '[':
temp = res[-1] + temp
res = res[:-1]
# Remove the opening bracket from the result string.
res = res[:-1]
# Extract the preceding number and convert it to an integer.
num = ""
while res and res[-1].isdigit():
num = res[-1] + num
res = res[:-1]
p = int(num)
# Append the substring to the result
#string, repeat it to the required number of times.
res += temp * p
return res
if __name__ == "__main__":
s = "3[b2[ca]]"
print(decodedString(s))
C#
using System;
using System.Text;
class GfG {
static string decodedString(string s) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < s.Length; i++) {
if (s[i] != ']')
res.Append(s[i]);
else {
StringBuilder temp = new StringBuilder();
while (res.Length > 0 && res[res.Length - 1] != '[') {
temp.Insert(0, res[res.Length - 1]);
res.Length--;
}
// Remove the opening bracket from the result string.
res.Length--;
// Extract the preceding number and convert it to an integer.
StringBuilder num = new StringBuilder();
while (res.Length > 0 && char.IsDigit(res[res.Length - 1])) {
num.Insert(0, res[res.Length - 1]);
res.Length--;
}
int p = int.Parse(num.ToString());
// Append the substring to the result string,
// repeat it to the required number of times.
for (int j = 0; j < p; j++)
res.Append(temp.ToString());
}
}
// Return the decoded string.
return res.ToString();
}
static void Main(string[] args) {
string s = "3[b2[ca]]";
Console.WriteLine(decodedString(s));
}
}
JavaScript
function decodedString(s) {
let res = "";
for (let i = 0; i < s.length; i++) {
if (s[i] !== ']') {
res += s[i];
}
else {
let temp = "";
while (res.length > 0 && res[res.length - 1] !== '[') {
temp = res[res.length - 1] + temp;
res = res.slice(0, -1);
}
// Remove the opening bracket from the result string.
res = res.slice(0, -1);
// Extract the preceding number and convert it to an integer.
let num = "";
while (res.length > 0 && !isNaN(res[res.length - 1])) {
num = res[res.length - 1] + num;
res = res.slice(0, -1);
}
let p = parseInt(num);
// Append the substring to the result string,
// repeat it to the required number of times.
res += temp.repeat(p);
}
}
// Return the decoded string.
return res;
}
// Driver Code
let s = "3[b2[ca]]";
console.log(decodedString(s));
Decode the string | DSA Problem
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem