Last updated on February 12th, 2025 at 01:59 am
Here, we see a Decode String LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Depth-First Search, Stack
Companies
Google, Yelp
Level of Question
Medium

Decode String LeetCode Solution
Table of Contents
1. Problem Statement
Given an encoded string, return its decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4].
The test cases are generated so that the length of the output will never exceed 105.
Example 1:
Input: s = “3[a]2[bc]”
Output: “aaabcbc”
Example 2:
Input: s = “3[a2[c]]”
Output: “accaccacc”
Example 3:
Input: s = “2[abc]3[cd]ef”
Output: “abcabccdcdcdef”
2. Coding Pattern Used in Solution
The coding pattern used in this code is “Stack-based String Decoding”. This pattern involves using a stack to decode a string that contains nested patterns of numbers and brackets, where the number indicates how many times the enclosed string should be repeated.
3. Code Implementation in Different Languages
3.1 Decode String C++
class Solution { public: string decodeString(string s) { stack<char> st; for(int i = 0; i < s.size(); i++){ if(s[i] != ']') { st.push(s[i]); } else{ string curr_str = ""; while(st.top() != '['){ curr_str = st.top() + curr_str ; st.pop(); } st.pop(); string number = ""; while(!st.empty() && isdigit(st.top())){ number = st.top() + number; st.pop(); } int k_time = stoi(number); while(k_time--){ for(int p = 0; p < curr_str.size() ; p++) st.push(curr_str[p]); } } } s = ""; while(!st.empty()){ s = st.top() + s; st.pop(); } return s; } };
3.2 Decode String Java
class Solution { public String decodeString(String s) { Stack<Character> stack = new Stack<>(); for(char c : s.toCharArray()) { if(c != ']') stack.push(c); else { StringBuilder sb = new StringBuilder(); while(!stack.isEmpty() && Character.isLetter(stack.peek())) sb.insert(0, stack.pop()); String sub = sb.toString(); stack.pop(); sb = new StringBuilder(); while(!stack.isEmpty() && Character.isDigit(stack.peek())) sb.insert(0, stack.pop()); int count = Integer.valueOf(sb.toString()); while(count > 0) { for(char ch : sub.toCharArray()) stack.push(ch); count--; } } } StringBuilder retv = new StringBuilder(); while(!stack.isEmpty()) retv.insert(0, stack.pop()); return retv.toString(); } }
3.3 Decode String JavaScript
var decodeString = function(s) { const stack = []; for (const char of s) { if (char !== "]") { stack.push(char); continue; } let cur = stack.pop(); let str = ''; while (cur !== '[') { str = cur + str; cur = stack.pop(); } let num = ''; cur = stack.pop(); while (!Number.isNaN(Number(cur))) { num = cur + num; cur = stack.pop(); } stack.push(cur); stack.push(str.repeat(Number(num))); } return stack.join(''); };
3.4 Decode String Python
class Solution(object): def decodeString(self, s): stack = []; curNum = 0; curString = '' for c in s: if c == '[': stack.append(curString) stack.append(curNum) curString = '' curNum = 0 elif c == ']': num = stack.pop() prevString = stack.pop() curString = prevString + num*curString elif c.isdigit(): curNum = curNum*10 + int(c) else: curString += c return curString
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(n * m) | O(n) |
Java | O(n * m) | O(n) |
JavaScript | O(n * m) | O(n) |
Python | O(n * m) | O(n) |
here, n
is the length of the string, m
is the maximum repeat count.