FastPrepDecode String
Problem · String

Decode String

Learn this problem
MediumGoogle logoGoogleFULLTIMEPHONE SCREENONSITE INTERVIEW
See Google hiring insights

Problem statement

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

The encoding rule is k[encoded_string], meaning that the content inside the brackets is repeated exactly k times. Encoded groups may be nested, and adjacent literal or encoded groups are concatenated.

For this exercise, assume repetition counts are positive decimal integers and literal characters are lowercase English letters.

Function

decodeString(s: String) → String

Examples

Example 1

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

The inner group 2[c] becomes cc, so the outer group is 3[acc].

Example 2

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

Decode the two repeated groups independently, then append the literal suffix ef.

Constraints

  • 1 <= s.length <= 10^5
  • s is a valid encoding with balanced brackets.
  • Every repetition count is in [1, 300].
  • Literal characters are lowercase English letters.
  • The decoded output length is at most 10^5.

More Google problems

drafts saved locally
public String decodeString(String s) {
    // Write your code here.
}
s"3[a2[c]]"
expected"accaccacc"
checking account