Intermediate Level

Implement the decodeEncodedStringSimple method that decodes a bracketed encoded string pattern.

The input contains an encoded string text. The encoding uses a repeat count followed by square brackets, such as 3[a], which means the text inside the brackets must be repeated three times.

Your task is to decode the complete string and return the expanded result. Nested encodings must also be handled correctly.

For example, 3[a]2[bc] becomes aaabcbc, and 3[a2[c]] becomes accaccacc.

Example 1
Input:
text (string) = 3[a]2[bc]
Return:
(string) aaabcbc
Example 2
Input:
text (string) = 3[a2[c]]
Return:
(string) accaccacc
Example 3
Input:
text (string) = 2[abc]3[cd]ef
Return:
(string) abcabccdcdcdef

Use stacks to remember the previous text and repeat count whenever an opening bracket is found.

Scan the string from left to right. Build the repeat number when digits are found. When [ appears, push the current decoded text and the repeat count, then start a new inner text. When ] appears, repeat the current inner text and attach it to the previous text from the stack.

This works naturally for nested patterns because the most recent opening bracket is completed first.

Pseudocode:

function decodeEncodedStringSimple(text):
    countStack = empty stack
    textStack = empty stack
    currentText = empty string
    currentNumber = 0
    for each character ch in text:
        if ch is a digit:
            currentNumber = currentNumber * 10 + digit value of ch
        else if ch == '[':
            push currentNumber into countStack
            push currentText into textStack
            currentNumber = 0
            currentText = empty string
        else if ch == ']':
            repeatCount = pop from countStack
            previousText = pop from textStack
            currentText = previousText + currentText repeated repeatCount times
        else:
            append ch to currentText
    return currentText
Run your code to see the result.