All problems
Decode String
Problem
Decode a string with repeated blocks written in k[encoded] form.
The encoded part inside brackets is repeated k times and blocks may nest. Use a stack of previously built strings and repeat counts: digits accumulate into the current count, an opening bracket saves the state, and a closing bracket pops and repeats the current string.
Return the fully decoded string.
Hint
Push the string built so far and the repeat count when entering a bracket; multiply on exit.
Test cases
case 1
in: ["3[a]2[bc]"]
expected: "aaabcbc"
case 2
in: ["3[a2[c]]"]
expected: "accaccacc"
case 3
in: ["2[abc]3[cd]ef"]
expected: "abcabccdcdcdef"
case 4
in: ["10[a]"]
expected: "aaaaaaaaaa"
case 5
in: ["abc"]
expected: "abc"
Starter codePython
def decode_string(s):
# Your code here
pass5 test cases · run your code against them in the editor