Decode String
Problem Statement
Given a string
containing only lowercase English letters and digits, and a string
encoded in a specific way, decode the string back into its original form.
The original string may have been encoded by using numbers and letters to represent repetitions of strings. Specifically, every substring that can be expressed as a repeat of a string will be expressed as a
followed by a
, where
is the number of repetitions and
is the string being repeated.
s = encoded_string, encoded_string = s[0]
if(i==0)
s = num + letter
else
s = s[0...num].repeat(num) + letter
Rules and Constraints
- The input will contain only lowercase English letters () and digits ().
- Numbers will not have leading zeros (i.e., is not a valid number in ).
- No invalid operations like divide by zero or taking the root of a negative number will occur.
- The output should be the decoded string.
Time Complexity
- O(n), where n is the length of , to traverse through the string once.
Space Complexity
- O(n), additional space to store the intermediate decoded string.
Note that these complexities assume the use of a stack to store the intermediate decoded strings.
Example
Input: {"s":"3[a2[c]]"}
Output: "accaccacc"