Goal Parser Interpretation (LeetCode 1678): Solution and Explanation

Easy
String

A string is built from the pieces G, () and (al). Decode each piece (G stays G, () becomes o, (al) becomes al) and return the joined result.

Intuition

This is a tiny tokenizer. The input is a sequence of tokens with no separators, so the job is to find where each token starts and ends, decode it, and move on.

Each token can be told apart by looking at most two characters. A G is a token on its own. A ( starts either () or (al), and the very next character settles which: ) means the short token, a means the long one.

Because every token has a fixed length (1, 2 or 4), the read index can jump straight past a decoded token. Nothing ever needs to be read twice, so one left-to-right pass is enough.

Approach: One-pass tokenizer with one character of lookahead

  1. Set i = 0 and create an empty output builder.
  2. While i < n: if command[i] is G, append G and add 1 to i.
  3. Otherwise peek at command[i + 1]. If it is ), append o and add 2 to i.
  4. Otherwise the token is (al): append al and add 4 to i.
  5. Return the joined output.

Step-by-step example

command = "G()(al)"
  1. i = 0: command[0] is G. Append G, move to i = 1.
  2. i = 1: command[1] is (, so peek at command[2], which is ). The token is (): append o, move to i = 3.
  3. i = 3: command[3] is ( and the peek finds a. The token is (al): append al, move to i = 7.
  4. i = 7 equals the length, so the loop ends and the answer is "Goal".

Code: Python, Java, C++

1class Solution:
2    def interpret(self, command: str) -> str:
3        out = []
4        i = 0
5        while i < len(command):
6            if command[i] == 'G':
7                out.append('G')
8                i += 1
9            elif command[i + 1] == ')':
10                out.append('o')
11                i += 2
12            else:
13                out.append('al')
14                i += 4
15        return ''.join(out)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

The index only moves forward, and every character is looked at a constant number of times.

Space

O(n)

The output string can be as long as the input (all G), so the builder uses linear space.

Other approaches

Chained replace (time O(n), space O(n))

Call command.replace("()", "o").replace("(al)", "al"). It is short and correct because the two patterns cannot overlap, but each replace makes a new string.

Character-driven builder (time O(n), space O(n))

Append G on G, and on ) append o if the previous character was ( and skip otherwise, copying a and l as they come. It works but is harder to read than tokenizing.

Common mistakes

  • Replacing (al) by stripping all parentheses first: () would then vanish instead of turning into o.
  • Advancing the index by 1 on a bracket token, which re-reads a, l or ) as if they started new tokens.
  • Building the answer with repeated string concatenation in a language where that copies the whole string each time. Use a builder or a list.

Frequently asked questions

Is it safe to read command[i + 1] without a bounds check?

Yes. When command[i] is (, the input is guaranteed to continue with ) or al), so index i + 1 always exists.

Does the order of the replace calls matter in the one-liner?

No. The patterns () and (al) never overlap, so replacing either first gives the same answer.

What is the time and space complexity?

O(n) time for one pass and O(n) space for the output string.