Goal Parser Interpretation - LeetCode

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.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b69ab494-a68f-4784-a4c3-2ad4fdf5d422/Screen_Shot_2021-04-06_at_12.03.54_PM.png

"""
1678. Goal Parser Interpretation

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.

Input: command = "G()(al)"
Output: "Goal"

Input: command = "G()()()()(al)"
Output: "Gooooal"

Input: command = "(al)G(al)()()G"
Output: "alGalooG"
"""

command = "(al)G(al)()()G"
result = ""

i = 0
while i < len(command):
    if command[i] == "(":
        if command[i+1] == ")":
            result += "o"

            i += 2
        elif command[i+1] == "a":
            result += "al"

            i += 4
    else:
        result += command[i]
        
        i += 1

print(result)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e709073a-d417-4a79-b77e-bacbaeb64885/Screen_Shot_2021-04-06_at_12.06.05_PM.png