Goal Parser Interpretation LeetCode Solution - The Coding Shala
Home >> LeetCode >> Goal Parser Interpretation
Other Posts You May Like
In this post, we will learn how to solve LeetCode's Goal Parser Interpretation Problem and will implement its solution in Java.
Goal Parser Interpretation Problem
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 the 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"
Practice this problem on LeetCode.
LeetCode - Goal Parser Interpretation Java Solution
Approach 1
We can check all three conditions.
Java Program:
class Solution { public String interpret(String command) { StringBuilder res = new StringBuilder(); int i = 0; while(i < command.length()) { if(command.charAt(i) == 'G') { res.append("G"); i++; } else if(command.charAt(i) == '(' && command.charAt(i+1) == ')') { res.append("o"); i = i+2; } else { res.append("al"); i = i+4; } } return res.toString(); } }
- LeetCode - Count Items Matching a Rule
- LeetCode - Check if the Sentence is Pangram
- LeetCode - Sorting Sentence
- LeetCode - Goat Latin
- LeetCode - Truncate Sentence
Comments
Post a Comment