Stack
Decode String
DESCRIPTION (inspired by Leetcode.com)
Given an encoded string, write a function to return its decoded string that follows a specific encoding rule: k[encoded_string], where the encoded_string within the brackets is repeated exactly k times. Note that k is always a positive integer. The input string is well-formed without any extra spaces, and square brackets are properly matched. Also, assume that the original data doesn't contain digits other than the ones that specify the number of times to repeat the following encoded_string.
Inputs:
s = "3[a2[c]]"
Output:
"accaccacc"
Explanation
def decodeString(s):stack = []curr_string = ""current_number = 0for char in s:if char == "[":stack.append(curr_string)stack.append(current_number)curr_string = ""current_number = 0elif char == "]":num = stack.pop()prev_string = stack.pop()curr_string = prev_string + num * curr_stringelif char.isdigit():current_number = current_number * 10 + int(char)else:curr_string += charreturn curr_string
decode string
0 / 1
"[": Start of a new sequence
def decodeString(s):stack = []curr_string = ""current_number = 0for char in s:if char == "[":stack.append(curr_string)stack.append(current_number)curr_string = ""current_number = 0elif char == "]":num = stack.pop()prev_string = stack.pop()curr_string = prev_string + num * curr_stringelif char.isdigit():current_number = current_number * 10 + int(char)else:curr_string += charreturn curr_string
[
0 / 2
"]": End of a sequence
def decodeString(s):stack = []curr_string = ""current_number = 0for char in s:if char == "[":stack.append(curr_string)stack.append(current_number)curr_string = ""current_number = 0elif char == "]":num = stack.pop()prev_string = stack.pop()curr_string = prev_string + num * curr_stringelif char.isdigit():current_number = current_number * 10 + int(char)else:curr_string += charreturn curr_string
]
0 / 1
Digit
def decodeString(s):stack = []curr_string = ""current_number = 0for char in s:if char == "[":stack.append(curr_string)stack.append(current_number)curr_string = ""current_number = 0elif char == "]":num = stack.pop()prev_string = stack.pop()curr_string = prev_string + num * curr_stringelif char.isdigit():current_number = current_number * 10 + int(char)else:curr_string += charreturn curr_string
initialize variables
0 / 2
Letter
Solution
def decodeString(s):stack = []curr_string = ""current_number = 0for char in s:if char == "[":stack.append(curr_string)stack.append(current_number)curr_string = ""current_number = 0elif char == "]":num = stack.pop()prev_string = stack.pop()curr_string = prev_string + num * curr_stringelif char.isdigit():current_number = current_number * 10 + int(char)else:curr_string += charreturn curr_string
decode string
0 / 20
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(S) where S is the length of the decoded output string. While we iterate through the input string once, constructing the repeated strings takes time proportional to the total number of characters in the final result.
Space Complexity: O(S) where S is the length of the decoded output string. We need to store the decoded string, and in the worst case, the stack can also grow proportional to the output size.
Mark as read
Your account is free and you can post anonymously if you choose.