Sobes.tech
Junior — Senior

Parsing commands for Goal Parser

livecode

Task condition

Given a string cmd, containing a sequence of commands for the Goal Parser program. Commands can only be of the following types: "G", "()" and "(al)". Their meanings are as follows:

  • "G" → "G"
  • "()" → "o"
  • "(al)" → "al" It is necessary to iterate through the string cmd, replace each found pattern according to the rules, and assemble the resulting string.

Example 1: Input: cmd = "Goal" Output: "Goal" Explanation:

  • "G" → "G"
  • the other characters "o", "a", "l" are already in the correct form and do not require replacement.
  • The final word remains "Goal".

Example 2: Input: cmd = "G()(al)" Output: "Goal" Explanation:

  • "G" → "G"
  • "()" → "o"
  • "(al)" → "al"
  • Concatenating parts gives "G" + "o" + "al" = "Goal".

Example 3: Input: cmd = "G()()()()(al)" Output: "Gooooal" Explanation:

  • "G" → "G"
  • each sequence "()" is transformed into "o" (four times → "oooo")
  • "(al)" → "al"
  • Final: "G" + "oooo" + "al" = "Gooooal".