Goal Parser Interpretation LeetCode Solution - The Coding Shala

Home >> LeetCode >> Goal Parser Interpretation

 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();
    }
}


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala