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"
Constraints:
1 <= s.length <= 30
s
consists of lowercase English letters, digits, and square brackets '[]'
.s
is guaranteed to be a valid input.s
are in the range [1, 300]
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The decode string problem can be solved by trying every possible repetition count and substring. We will exhaustively explore all combinations of repeating and concatenating decoded segments until we find the fully decoded string. This approach is not efficient, but it's a straightforward way to understand the problem.
Here's how the algorithm would work step-by-step:
def decode_string_brute_force(encoded_string):
while True:
start_index = -1
for index, char in enumerate(encoded_string):
if char.isdigit():
start_index = index
break
if start_index == -1:
break
number_end_index = start_index
while number_end_index < len(encoded_string) and encoded_string[number_end_index].isdigit():
number_end_index += 1
repetition_count = int(encoded_string[start_index:number_end_index])
# Find the corresponding opening bracket
bracket_start = number_end_index
bracket_level = 1
bracket_end = bracket_start + 1
# We need to find the end of the encoded segment
while bracket_end < len(encoded_string) and bracket_level > 0:
if encoded_string[bracket_end] == '[':
bracket_level += 1
elif encoded_string[bracket_end] == ']':
bracket_level -= 1
bracket_end += 1
bracket_end -= 1
repeated_string = encoded_string[bracket_start + 1:bracket_end] * repetition_count
encoded_string = encoded_string[:start_index] + repeated_string + encoded_string[bracket_end+1:]
return encoded_string
The best way to decode this string is to use the concept of a stack. We'll process the string character by character, using the stack to remember what we've seen and properly handle nested encoded sequences.
Here's how the algorithm would work step-by-step:
def decode_string(encoded_string):
string_stack = []
current_string = ''
current_number = 0
for char in encoded_string:
if char.isdigit():
current_number = current_number * 10 + int(char)
elif char == '[':
# Save current state before diving into substring
string_stack.append((current_string, current_number))
current_string = ''
current_number = 0
elif char == ']':
# Retrieve previous state to build decoded string
previous_string, repeat_count = string_stack.pop()
current_string = previous_string + current_string * repeat_count
else:
# Append chars to current substring
current_string += char
return current_string
Case | How to Handle |
---|---|
Empty string input | Return an empty string as there's nothing to decode. |
Null string input | Throw an IllegalArgumentException or return null to signal an invalid input. |
Input string with no encoding (no numbers or brackets) | Return the original string unchanged as it represents a simple string. |
Nested encodings with high repetition counts (e.g., 1000[abc]) to test performance | The solution should scale reasonably, potentially using StringBuilder for efficient string concatenation to avoid quadratic time complexity. |
Invalid format with mismatched brackets (e.g., 3[ab or 3ab]) | Throw an IllegalArgumentException or return an error string to indicate invalid input. |
Very deep nesting of encodings (e.g., many layers of [ and ]) to test stack overflow | An iterative approach avoids recursion depth limits, or optimize recursive solution to be tail recursive if language supports it. |
Repetition factor of 0 (e.g., 0[abc]) | Treat it as an empty string, so '0[abc]' should decode to ''. |
Large repetition factors that could lead to integer overflow when calculating final string length | Implement overflow checks, or switch to using long type for intermediate calculations to avoid integer overflow related issues. |