Decode String

Medium Stack String

Problem

Decode an encoded string where k[encoded_string] means the substring inside the brackets is repeated k times. Encodings may nest.

Original: LeetCode 394: Decode String.

Examples

Example 1

Input`s = "3[a]2[bc]"`
Output`"aaabcbc"`

Example 2

Input`s = "3[a2[c]]"`
Output`"accaccacc"`

Example 3

Input`s = "2[abc]3[cd]ef"`
Output`"abcabccdcdcdef"`

Constraints

  • 1 <= s.length <= 30
  • s consists of lowercase English letters, digits, and square brackets '[]'.
  • s is guaranteed to be a valid input.
  • All the integers in s are in the range [1, 300].

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (1)
  1. LeetCode 394: Decode Stringleetcode.com