FastPrepDecode String

Decode String

Abridge logoAbridge● MediumFULLTIMEOA
Learn

Problem statement

Given a valid encoded string s, return its decoded form.

An encoded group has the form k[encodedString], meaning that encodedString is repeated exactly k times. Groups may be nested, and a repeat count may contain multiple digits.

The unencoded text contains lowercase English letters. Digits appear only as repeat counts immediately before bracketed groups.

Function

decodeString(s: String) → String

Examples

Example 1

s = "3[a]2[bc]"return = "aaabcbc"

Repeat a three times and bc twice, then concatenate the two decoded parts.

Example 2

s = "3[a2[c]]"return = "accaccacc"

The inner group becomes cc, so the outer group repeats acc three times.

Example 3

s = "2[abc]3[cd]ef"return = "abcabccdcdcdef"

Decode the two adjacent repeated groups, then retain the trailing literal ef.

Constraints

  • 1 <= s.length <= 30.
  • s contains lowercase English letters, digits, and square brackets.
  • s is a valid encoding with well-formed brackets.
  • Every repeat count is between 1 and 300.
  • The decoded output length does not exceed 10^5.

More Abridge problems

See Abridge hiring insights
public String decodeString(String s) {
    // Write your code here.
}
s"3[a]2[bc]"
expected"aaabcbc"
Checking account…