Search⌘ K
AI Features

Problem: Decode String

Explore how to decode nested encoded strings that use a format of repeat counts and brackets by applying stack data structures. Understand step-by-step how to parse strings, handle nested patterns, and reconstruct the decoded output. This lesson helps you implement a robust solution to decode strings with nested repetitions while managing time and space complexity effectively.

Statement

Given an encoded string s, return its decoded string.

The encoding rule follows the format k[encoded_string], where the encoded_string enclosed within the square brackets is repeated exactly k times. The value k is guaranteed to be a positive integer.

You may assume that the input string is always valid: there are no extra whitespace characters, and all square brackets are well formed. Additionally, the original data contains no digit characters; digits appear only as repeat counts (k). For instance, inputs such as 3a or 2[4] will not occur.

The test cases are constructed such that the length of the decoded output never exceeds 10510^5.

Note: Nested encodings are possible, such as k1[string1 k2[string2]], where inner patterns are decoded first before the outer repetition is applied.

Constraints:

  • 11 \leq s.length 30\leq 30

  • s consists of lowercase English letters, digits, and square brackets '[' and ']'

  • s is guaranteed to be a valid input

  • All integers in s are in the range [1,300][1, 300] ...