LeetCode Challenge: Goal Parser Interpretation

Description

You own a Goal Parser that can interpret a string . The consists of an alphabet of , and/or in some order. The Goal Parser will interpret as the string , as the string , and as the string . The interpreted strings are then concatenated in the original order.

Given the string , return the Goal Parser's interpretation of .

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

  • consists of , , and/or 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.