Google questions

Nested String Decompression

GoogleOnsiteMedium

A compressed string is built from lowercase letters and groups written (text){k}. A group stands for text repeated k times, and text may itself contain more groups.

Part 1: Decompress the string

You are given a compressed string s made of lowercase letters and groups written (text){k}. A group stands for text repeated k times, and text may itself contain more groups. Return the decompressed string.

s is well formed: every ( is closed by a ) that is followed by {k}, each group holds at least one character, and every other character is a lowercase letter. An empty s decompresses to an empty string.

  1. Example 1
    s
    "a(abc){3}"
    Output
    "aabcabcabc"

    Why: The group (abc) repeats three times after the leading a.

  2. Example 2
    s
    "a(b(c){2}){3}d"
    Output
    "abccbccbccd"

    Why: The inner group (c){2} makes cc, so the outer group expands to bcc repeated three times between a and d.

Constraints

  • 0 <= len(s) <= 10^4
  • 2 <= k <= 99 for every group
  • the decompressed string has at most 10^5 characters
  • each ( is closed by a ) followed by {k}
  • each group's text is non-empty
  • every character of s outside (, ), {, } and the digits of k is a lowercase letter
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Nested String Decompression: Google interview question | LoopCoach