Problem · String

Decode Repeated Groups

MediumWaymoFULLTIMEPHONE SCREEN

Complete the function below. The function receives the full standard input as a single string and returns the exact standard output lines.

Problem

Decode strings that contain repeated parenthesized groups. Plain characters are copied directly. A group has the form (substring){k}, meaning the decoded substring inside the parentheses is repeated k times. Groups may be nested.

For each encoded string, output its decoded form.

Function Description

Complete solveDecodeRepeatedGroups. It has one parameter, String input. The first line is q, followed by q encoded strings. Return one decoded string per query.

Examples
01 · Example 1
input = "3\nabs(cs){3}g\na(b(c){2}){2}\nx(yz){0}q"
return = ["abscscscsg","abccbcc","xq"]

The second case decodes the nested group b(c){2} as bcc, then repeats it twice.

Constraints

Repeat counts are non-negative integers written inside braces after a closing parenthesis.

Parentheses and braces are balanced in valid inputs.

More Waymo problems
drafts saved locally
public String[] solveDecodeRepeatedGroups(String input) {
    // write your code here
}
input"3\nabs(cs){3}g\na(b(c){2}){2}\nx(yz){0}q"
expected["abscscscsg", "abccbcc", "xq"]
sign in to submit