Goal Parser Interpretation (LeetCode 1678): Solution and Explanation
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
- Set
i = 0and create an empty output builder. - While
i < n: ifcommand[i]isG, appendGand add 1 toi. - Otherwise peek at
command[i + 1]. If it is), appendoand add 2 toi. - Otherwise the token is
(al): appendaland add 4 toi. - Return the joined output.
Step-by-step example
command = "G()(al)"
i = 0:command[0]isG. AppendG, move toi = 1.i = 1:command[1]is(, so peek atcommand[2], which is). The token is(): appendo, move toi = 3.i = 3:command[3]is(and the peek findsa. The token is(al): appendal, move toi = 7.i = 7equals 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)1class Solution {
2 public String interpret(String command) {
3 StringBuilder out = new StringBuilder();
4 int i = 0;
5 while (i < command.length()) {
6 if (command.charAt(i) == 'G') {
7 out.append('G');
8 i += 1;
9 } else if (command.charAt(i + 1) == ')') {
10 out.append('o');
11 i += 2;
12 } else {
13 out.append("al");
14 i += 4;
15 }
16 }
17 return out.toString();
18 }
19}1class Solution {
2public:
3 string interpret(string command) {
4 string out;
5 size_t i = 0;
6 while (i < command.size()) {
7 if (command[i] == 'G') {
8 out += 'G';
9 i += 1;
10 } else if (command[i + 1] == ')') {
11 out += 'o';
12 i += 2;
13 } else {
14 out += "al";
15 i += 4;
16 }
17 }
18 return out;
19 }
20};Time and space complexity
O(n)
The index only moves forward, and every character is looked at a constant number of times.
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 intoo. - Advancing the index by 1 on a bracket token, which re-reads
a,lor)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.