Skip to content

Instantly share code, notes, and snippets.

@pjcodesjs
Created May 4, 2021 18:28
Show Gist options
  • Save pjcodesjs/d6112309c60b0b0b6102fcc6877c909b to your computer and use it in GitHub Desktop.
Save pjcodesjs/d6112309c60b0b0b6102fcc6877c909b to your computer and use it in GitHub Desktop.
PJ Codes Leet code - 1678. Goal Parser Interpretation
/*
LEETCODE PROBLEM: 1678. Goal Parser Interpretation
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 "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"
----------
EXAMPLE 3:
----------
Input: command = "(al)G(al)()()G"
Output: "alGalooG"
*/
let interpret = (command) => {
let results = [];
let split_command = command.split('');
for (let i = 0; i < split_command.length; i++) {
split_command[i] === 'G' ? results.push(split_command[i]) : split_command[i] === '(' && split_command[i + 1] == ')' ? results.push('o') : split_command[i] === '(' && split_command[i + 1] != ')' ? results.push('al') : null;
}
return results.join('');
}
interpret("(al)G(al)()()G");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment