Goal Parser Interpretation
EasyUpdated: Aug 2, 2025
Practice on:
Problem
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 _theGoal Parser 's interpretation of _command.
Examples
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 <= 100commandconsists of"G","()", and/or"(al)"in some order.
Solution
Method 1 – String Replacement
Intuition
The problem is a simple string parsing task. We can directly replace the patterns "()" with "o" and "(al)" with "al" in the input string, as they do not overlap and are unique.
Approach
- Replace all occurrences of "(al)" with "al" in the input string.
- Replace all occurrences of "()" with "o" in the result.
- Return the final string.
Code
C++
class Solution {
public:
string interpret(string command) {
string ans;
for (size_t i = 0; i < command.size(); ) {
if (command[i] == 'G') {
ans += 'G';
i++;
} else if (command.substr(i, 2) == "()") {
ans += 'o';
i += 2;
} else if (command.substr(i, 4) == "(al)") {
ans += "al";
i += 4;
}
}
return ans;
}
};
Go
func interpret(command string) string {
ans := ""
for i := 0; i < len(command); {
if command[i] == 'G' {
ans += "G"
i++
} else if i+1 < len(command) && command[i:i+2] == "()" {
ans += "o"
i += 2
} else if i+3 < len(command) && command[i:i+4] == "(al)" {
ans += "al"
i += 4
}
}
return ans
}
Java
class Solution {
public String interpret(String command) {
StringBuilder ans = new StringBuilder();
for (int i = 0; i < command.length(); ) {
if (command.charAt(i) == 'G') {
ans.append('G');
i++;
} else if (command.startsWith("()", i)) {
ans.append('o');
i += 2;
} else if (command.startsWith("(al)", i)) {
ans.append("al");
i += 4;
}
}
return ans.toString();
}
}
Kotlin
class Solution {
fun interpret(command: String): String {
val sb = StringBuilder()
var i = 0
while (i < command.length) {
when {
command[i] == 'G' -> {
sb.append('G')
i++
}
command.startsWith("()", i) -> {
sb.append('o')
i += 2
}
command.startsWith("(al)", i) -> {
sb.append("al")
i += 4
}
}
}
return sb.toString()
}
}
Python
class Solution:
def interpret(self, command: str) -> str:
return command.replace("(al)", "al").replace("()", "o")
Rust
impl Solution {
pub fn interpret(command: String) -> String {
command.replace("(al)", "al").replace("()", "o")
}
}
TypeScript
class Solution {
interpret(command: string): string {
return command.replace(/\(al\)/g, "al").replace(/\(\)/g, "o");
}
}
Complexity
- ⏰ Time complexity:
O(n)— Each character is visited at most once. - 🧺 Space complexity:
O(n)— Output string is proportional to input size.