TIP102 Unit 7 Session 1 Advanced (Click for link to problem statements)
In the mystical city of Atlantis, ancient scrolls have been discovered that contain encoded messages. These messages follow a specific encoding rule: k[encoded_message]
, where the encoded_message
inside the square brackets is repeated exactly k
times. Your task is to decode these messages to reveal their original form using a recursive approach.
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
k[encoded_message]
where the message is repeated k
times.HAPPY CASE
Input: "3[Coral2[Shell]]"
Output: "CoralShellShellCoralShellShellCoralShellShell"
Explanation: The string "CoralShellShell" is repeated 3 times.
Input: "2[Poseidon3[Sea]]"
Output: "PoseidonSeaSeaSeaPoseidonSeaSeaSea"
Explanation: The string "PoseidonSeaSeaSea" is repeated 2 times.
EDGE CASE
Input: "4[Fish]"
Output: "FishFishFishFish"
Explanation: The string "Fish" is repeated 4 times without any nested encoding.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Decoding Encoded Strings, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Recursive Approach:
1) Define a helper function `helper(index)` that decodes the string starting from the given index.
2) Initialize an empty string `decoded_string` to store the decoded message.
3) While scanning the string:
a) If a digit is encountered, extract the full number.
b) Skip the `[` and recursively decode the substring within the brackets.
c) Multiply the decoded substring by the number and append it to `decoded_string`.
d) If `]` is encountered, return the current `decoded_string` and the updated index.
4) Return the fully decoded string from the main function `decode_scroll_recursive`.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def decode_scroll_recursive(scroll):
def helper(index):
decoded_string = "
while index < len(scroll):
if scroll[index].isdigit():
# Extract the full number (could be multiple digits)
num = 0
while index < len(scroll) and scroll[index].isdigit():
num = num * 10 + int(scroll[index])
index += 1
# The next character should be '[', so move to the character after it
index += 1
# Recursively decode the substring within the brackets
decoded_substring, index = helper(index)
# Multiply the decoded substring by the number and add it to the result
decoded_string += decoded_substring * num
elif scroll[index] == ']':
# Return the decoded string and the current index when encountering a closing bracket
return decoded_string, index + 1
else:
# Regular character, just add to the result string
decoded_string += scroll[index]
index += 1
return decoded_string, index
# Start the recursive decoding from index 0
final_decoded_string, _ = helper(0)
return final_decoded_string
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
decode_scroll_recursive
function with the input "3[Coral2[Shell]]"
. The function should return "CoralShellShellCoralShellShellCoralShellShell"
after correctly decoding the nested structure."4[Fish]"
. The function should return "FishFishFishFish"
.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
, where N
is the length of the string. The function processes each character exactly once.O(N)
, due to the recursion stack and the space needed to store the decoded string. The depth of recursion is proportional to the nesting level of the encoded messages.