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
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!
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code