LeetCode Challenge: Goal Parser Interpretation
2 min readDec 10, 2020
Description
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
.
Example 1
Input: command = "G()(al)"
Output: "Goal"
Explanation: The Goal Parser interprets the command as follows:
G -> G
() -> o
(al) -> al
The final concatenated result is "Goal".
Example 2
Input: command = "G()()()()(al)"
Output: "Gooooal"
Constraints
1 <= command.length <= 100
command
consists of"G"
,"()"
, and/or"(al)"
in some order.
Solution:
Steps
1. Declare a output variable, to hold the value of each character match in the command variable2. Split command variable into an array, then iterate through each character in that array.3. If we see the character 'G', then add G to the output variable4. If we see the character '(', the code will then run the nested if statement5. If there is a ')' character at the following index, then we add 'o' to the output variable6. If there is are 'a', 'l', and ')' characters in the following 3, index of the command variable then we add 'al' to the output variable7. When we are done we return the output variable
Code(Ruby)
Results(Ruby)
# Runtime: 68 ms, faster than 72.00% of Ruby online submissions for Goal Parser Interpretation.# Memory Usage: 209.8 MB, less than 56.00% of Ruby online submissions for Goal Parser Interpretation.
Code(JavaScript)
Results(JavaScript)
Runtime: 80 ms, faster than 74.13% of JavaScript online submissions for Goal Parser Interpretation.Memory Usage: 38.7 MB, less than 29.87% of JavaScript online submissions for Goal Parser Interpretation.
Thanks for viewing. If you have any feedback on how to improve this code, leave a comment.