Goal Parser Interpretation - 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"
  • "(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 & Output

Example 1 — Basic Mixed Command
$ Input: command = "G()(al)"
Output: "Goal"
💡 Note: G stays G, () becomes o, (al) becomes al. Concatenated: G + o + al = "Goal"
Example 2 — Multiple Patterns
$ Input: command = "G()()()()(al)"
Output: "Goooal"
💡 Note: G stays G, each () becomes o (4 times), (al) becomes al. Result: G + o + o + o + o + al = "Goooal"
Example 3 — Only Parentheses
$ Input: command = "(al)G(al)()()G"
Output: "alGalooG"
💡 Note: (al)→al, G→G, (al)→al, ()→o, ()→o, G→G. Concatenated: "alGalooG"

Constraints

  • 1 ≤ command.length ≤ 100
  • command consists of "G", "()", and/or "(al)" in some order

Visualization

Tap to expand
Goal Parser Interpretation INPUT Command String: G ( ) ( a l ) Parsing Rules: "G" --> "G" "()" --> "o" "(al)" --> "al" command = "G()(al)" ALGORITHM STEPS 1 Initialize Result Start with empty string 2 Replace "()" "()" --> "o" 3 Replace "(al)" "(al)" --> "al" 4 Keep "G" unchanged "G" stays as "G" Transformation: "G()(al)" "G" + "o" + "al" "Goal" Step 1 Step 2-4 Result FINAL RESULT Interpreted String: G o a l Character Mapping: "G" --> "G" "()" --> "o" "(al)" --> "al" Output: "Goal" OK Key Insight: String replacement is the simplest approach - replace "()" with "o" and "(al)" with "al". "G" remains unchanged. The order of replacements matters: replace "()" before "(al)" to avoid conflicts. Time Complexity: O(n) | Space Complexity: O(n) where n is the length of command string. TutorialsPoint - Goal Parser Interpretation | String Replacement Approach
Asked in
Amazon 15 Microsoft 12 Google 8
29.4K Views
Medium Frequency
~5 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen