You own a Goal Parser that can interpret a string command. The command consists of an alphabet of "G", "()" and/or "(al)" in some order. The Goal Parser will interpret "G" as the string "G", "()" as the string "o", and "(al)" as the string "al". The interpreted strings are then concatenated in the original order.
Given the string command, return the Goal Parser's interpretation of command.
Example 1:
Input: command = "G()(al)" Output: "Goal" Explanation: The Goal Parser interprets the command as follows: G -> G () -> o (al) -> al The final concatenated result is "Goal".
Example 2:
Input: command = "G()()()()(al)" Output: "Gooooal"
Example 3:
Input: command = "(al)G(al)()()G" Output: "alGalooG"
Constraints:
1 <= command.length <= 100command consists of "G", "()", and/or "(al)" in some order.Problem Overview: You receive a command string used by the Goal Parser. The parser interprets patterns using simple rules: "G" -> "G", "()" -> "o", and "(al)" -> "al". The task is to scan the input command and return the interpreted string after applying these mappings.
This problem is fundamentally a string parsing task. The input contains predictable patterns, so the goal is simply to recognize them efficiently while traversing the string once.
Approach 1: String Replacement (O(n) time, O(n) space)
The most direct solution replaces the known patterns with their interpreted values. Since the command only contains three valid tokens (G, (), and (al)), you can apply sequential string replacements: replace () with o and (al) with al. After replacements, the remaining characters are already valid output.
Another variation scans the string using an index pointer. When you encounter 'G', append it to the result. If you see '(', check the next characters: () becomes "o", while (al) becomes "al". Move the pointer accordingly. Each character is processed once, giving O(n) time complexity and O(n) space for the output string.
This approach works because the grammar is fixed and non-overlapping. There is no ambiguity, so simple conditional checks or replacement operations are sufficient. For interview settings, the pointer-based scan demonstrates clear understanding of string manipulation and avoids unnecessary intermediate copies.
Approach 2: Pattern Matching with Regular Expressions (O(n) time, O(n) space)
You can also solve the problem using regular expressions. Define a pattern that matches \(\) and \(al\), then replace them with their interpreted values. Regex engines efficiently scan the string and apply substitutions in a single pass.
For example, perform two replacements: convert () to "o", then convert (al) to "al". Most modern regex implementations run in linear time for this type of fixed pattern matching, resulting in O(n) time complexity with O(n) additional space for the transformed string.
This approach is concise and expressive, especially in languages like Python or JavaScript where regex replacements are built-in. However, interviewers often prefer explicit parsing logic because it demonstrates control over string traversal and avoids reliance on regex libraries.
Recommended for interviews: Use the pointer-based string scan from the String Replacement approach. It processes the input once in O(n) time with O(n) space, clearly shows how you identify tokens like () and (al), and avoids unnecessary library calls. The regex approach is shorter but hides the parsing logic, which interviewers usually want to see.
In this approach, the idea is to replace all occurrences of '()' with 'o' and '(al)' with 'al' in the command string. Since 'G' should remain unchanged, we can use direct replacements to generate the interpreted output.
We can perform these replacements in a single pass, or even within a loop through the string, where we gradually build the output string by detecting patterns and appending the respective characters.
The function iterates over the input string command. It uses a character array result to build the output string.
result.result and skips the next character with i++.result and skips the next three characters with i += 3.C
C++
Java
Python
C#
JavaScript
Go
TypeScript
Rust
Time Complexity: O(n), where n is the length of the command string, since each character is processed at most once.
Space Complexity: O(1), with the result array being the only additional space used, which is directly proportional in size to the input string.
In this approach, we utilize regular expressions to match and replace the specific patterns in the command string. This is a more concise approach that is generally supported by languages like Python and can simplify the transformation steps considerably.
We can replace all instances of '()' with 'o' and '(al)' with 'al' using regular expressions, then return the transformed string.
This Python solution employs the re.sub function from the regular expressions library to perform sequential replacements:
re.sub(r'\(\)', 'o', command) replaces '()' with 'o'.re.sub(r'\(al\)', 'al', command) replaces '(al)' with 'al'.command.Python
JavaScript
Time Complexity: O(n), as the regular expression operations on a string with n-length have similar complexity.
Space Complexity: O(n), given the construction of a new string.
We can also iterate over the string command. For each character c:
'G', directly add c to the result string;'(', check if the next character is ')'. If it is, add 'o' to the result string. Otherwise, add "al" to the result string.After the iteration, return the result string.
The time complexity is O(n), and the space complexity is O(1).
| Approach | Complexity |
|---|---|
| String Replacement | Time Complexity: O(n), where n is the length of the command string, since each character is processed at most once. Space Complexity: O(1), with the result array being the only additional space used, which is directly proportional in size to the input string. |
| Pattern Matching with Regular Expressions | Time Complexity: O(n), as the regular expression operations on a string with n-length have similar complexity. Space Complexity: O(n), given the construction of a new string. |
| String Iteration | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Replacement / Pointer Scan | O(n) | O(n) | Best general solution. Clear logic and preferred in coding interviews. |
| Regular Expression Replacement | O(n) | O(n) | Useful for concise implementations when regex support is convenient. |
1678 Goal Parser Interpretation • PNT Coding • 2,092 views views
Watch 9 more video solutions →Practice Goal Parser Interpretation with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor