Problem · String
Decode an Encoded String
Learn this problemProblem statement
An encoded string uses positive repeat counts followed by bracketed segments. Decode it using these rules:
k[segment]means the decoded segment is repeated exactlyktimes.- Segments may be nested.
- Letters outside brackets appear once and remain in order.
Return the fully decoded string.
Function
decodeString(s: String) → StringExamples
Example 1
s = "3[a2[c]]"return = "accaccacc"The inner block becomes acc, then the outer count repeats it three times.
Example 2
s = "2[ab]3[c]"return = "ababccc"The two adjacent encoded blocks decode independently and are concatenated.
Constraints
1 <= s.length <= 10^4sis a well-formed encoding made of lowercase English letters, digits, and brackets.- Every repeat count is between
1and300. - The decoded string has at most
10^5characters.