Goal Parser Interpretation - Problem

Imagine you've built a Goal Parser - a simple interpreter that can decode a custom command language! 🎯

Your parser understands exactly three symbols:

  • "G" β†’ translates to "G"
  • "()" β†’ translates to "o"
  • "(al)" β†’ translates to "al"

Given a command string containing only these symbols, your task is to interpret each symbol and concatenate the results in the original order.

Example: "G()(al)" becomes "Goal" because:

  • G β†’ G
  • () β†’ o
  • (al) β†’ al
  • Result: G + o + al = "Goal"

Input & Output

example_1.py β€” Basic Goal Parser
$ Input: command = "G()(al)"
β€Ί Output: "Goal"
πŸ’‘ Note: The parser reads 'G' (stays 'G'), then '()' (becomes 'o'), then '(al)' (becomes 'al'), resulting in 'Goal'
example_2.py β€” Multiple Patterns
$ Input: command = "G()()()()(al)"
β€Ί Output: "Goooal"
πŸ’‘ Note: G stays G, each () becomes o (four times), and (al) becomes al, creating 'Goooal'
example_3.py β€” Only G Characters
$ Input: command = "(al)G(al)()()G"
β€Ί Output: "alGalooG"
πŸ’‘ Note: (al)β†’al, Gβ†’G, (al)β†’al, ()β†’o, ()β†’o, Gβ†’G, resulting in 'alGalooG'

Constraints

  • 1 ≀ command.length ≀ 100
  • command consists of "G", "()", and/or "(al)" only
  • The input is guaranteed to be valid (no malformed patterns)

Visualization

Tap to expand
🎯 Goal Parser Translation MachineInput CommandG ( ) ( a l )ParserRule 1G β†’ GParserRule 2() β†’ oParserRule 3(al) β†’ alGoalConcatenated ResultGoal
Understanding the Visualization
1
Input Recognition
The parser scans the command and identifies each symbol pattern
2
Symbol Translation
Each recognized pattern gets translated: G→G, ()→o, (al)→al
3
Result Assembly
All translated symbols are concatenated in their original order
4
Output Generation
The final interpreted string is returned
Key Takeaway
🎯 Key Insight: The Goal Parser works like a simple finite state machine - when we see 'G' we output 'G', when we see '(' we look ahead to determine if it's '()' (output 'o') or '(al)' (output 'al'). This can be solved efficiently with either string replacement or single-pass parsing!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
98.6K Views
High Frequency
~8 min Avg. Time
2.8K 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