FastPrepParenthesized String Decompression

Parenthesized String Decompression

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Decompress a valid encoded string encoded.

A repeated group has the form (content){count}, where count is an integer from 2 through 99. Replace the complete group with content repeated count times.

Groups may be nested, and literal lowercase letters may appear before, after, or between groups. Return the fully decompressed string.

Function

decompress(encoded: String) → String

Examples

Example 1

encoded = "a(abc){3}"return = "aabcabcabc"

The group abc is repeated three times after the initial a.

Example 2

encoded = "a(b(c){2}){3}d"return = "abccbccbccd"

The inner group produces cc, then bcc is repeated three times.

Example 3

encoded = "(x){2}(yz){3}"return = "xxyzyzyz"

Adjacent encoded groups are expanded and concatenated.

Constraints

  • 1 <= encoded.length <= 100000.
  • The input is valid under the grammar above.
  • Literal text contains lowercase English letters.
  • Every count is in [2, 99] and appears immediately after a right parenthesis.
  • Nesting depth is at most 200.
  • The decompressed result has length at most 200000.

More Google problems

See Google hiring insights
public String decompress(String encoded) {
    // write your code here
}
encoded"a(abc){3}"
expected"aabcabcabc"
Checking account…