1. Decode the String
The problem can be found at the following link: Question Link
Problem Description
Given an encoded string s
, the task is to decode it.
The encoding rule is:
k[encoded_string]
, where the encoded string inside the square brackets is repeated exactlyk
times.Note:
k
is guaranteed to be a positive integer.
Examples
Example 1:
Input:
s = "1[b]"
Output:
b
Explanation:
The string "b" is present only once.
Example 2:
Input:
s = "3[b2[ca]]"
Output:
bcacabcacabcaca
Explanation:
First,
2[ca]
expands tocaca
Then,
3[bcaca]
expands tobcacabcacabcaca
which is the final output.
Constraints:
$(1 \leq |s| \leq 10^5)$
My Approach
Stack-Based Iterative Approach (O(N) Time, O(N) Space)
The goal is to parse and decode nested patterns like k[encoded_string]
using a stack-based simulation.
Algorithm Steps:
Use two stacks:
One for tracking the string built so far.
One for tracking the repetition counts (
k
).
Iterate over the string:
If you see a digit, accumulate it into
k
.If you see
[
, push the current string and currentk
to the stacks, then reset them.If you see
]
, pop the previous string andk
, and append the current string repeatedk
times.Otherwise, append the character to the current string.
The final decoded string will be the result.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), since each character is processed at most twice (once when pushed, once when popped).
Expected Auxiliary Space Complexity: O(N), as the worst case (deeply nested or large repeated sections) can push every character onto the stack.
Code (C++)
class Solution {
public:
string decodedString(string &s) {
stack<string> str;
stack<int> num;
string cur = "", temp;
int n = 0;
for (char c : s) {
if (isdigit(c)) n = n * 10 + (c - '0');
else if (c == '[') { str.push(cur); num.push(n); cur = ""; n = 0; }
else if (c == ']') {
temp = cur;
cur = str.top(); str.pop();
for (int i = 0, x = num.top(); i < x; i++) cur += temp;
num.pop();
} else cur += c;
}
return cur;
}
};
Code (Java)
class Solution {
static String decodeString(String s) {
Stack<String> str = new Stack<>();
Stack<Integer> num = new Stack<>();
String cur = "";
int n = 0;
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) n = n * 10 + (c - '0');
else if (c == '[') { str.push(cur); num.push(n); cur = ""; n = 0; }
else if (c == ']') {
String temp = cur;
cur = str.pop();
cur += temp.repeat(num.pop());
} else cur += c;
}
return cur;
}
}
Code (Python)
class Solution:
def decodedString(self, s: str) -> str:
str_st, num_st, cur, n = [], [], "", 0
for c in s:
if c.isdigit():
n = n * 10 + int(c)
elif c == "[":
str_st.append(cur)
num_st.append(n)
cur, n = "", 0
elif c == "]":
cur = str_st.pop() + cur * num_st.pop()
else:
cur += c
return cur
Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated