← 返回 google 的题目列表Decode an Encoded String
类型:online_judge
Given an encoded string s, decode it according to the following rule and output the full decoded string:
An encoding has the form k[encoded_string], meaning that the string inside the brackets is repeated k times.
k is a positive integer and may contain multiple digits.
Encodings may be nested.
The input is guaranteed to be valid. The original strings contain no digits; digits are used only as repetition counts.
Implement a decoder.
Example 1:
Input: 3[a]2[bc]
Output: aaabcbc
Example 2:
Input: 3[a2[c]]
Output: accaccacc
Example 3:
Input: 2[abc]3[cd]ef
Output: abcabccdcdcdef
Constraints:
1 <= len(s) <= 10^5
Repetition counts are positive integers.
The decoded output length is at most 10^6.
Follow-up: How would you redesign the solution to reduce extra memory usage if the decoded string can be too large to fit in memory at once?
Example
Input
3[a]2[bc]
Output
aaabcbc