Goal

 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.





 class Solution {

public:

    string interpret(string command) {

        string res;

        for(int i=0;i<command.size();i++){

            if(command[i]=='G'){

                res = res +'G';

            }

            else if(command[i]=='(' && command[i+1]==')' ){

                res = res + 'o';

                i=i+1;

            }

            else if(command[i]=='(' && command[i+1]=='a' ){

                res = res + "al";

                i=i+3;

            }

        }

        return res;

    }

};

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings