Goat Latin - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Wednesday 19 August 2020

Goat Latin

Goat Latin

Goat Latin

A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.

We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)

The rules of Goat Latin are as follows:

  • If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
    For example, the word 'apple' becomes 'applema'.
     
  • If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma".
    For example, the word "goat" becomes "oatgma".
     
  • Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.
    For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.

Return the final sentence representing the conversion from S to Goat Latin. 

 

Example 1:

Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"



 class Solution {

public:

    vector<string> split(string s)

    {

        vector<string> ans;

        string w="";

        for(auto ch :s)

        {

            if(ch==' ')

            {

                ans.push_back(w);

                w="";

            }else{

                w+=ch;

            }

        }

        ans.push_back(w);

        return ans;

    }

    string toGoatLatin(string S) {

        string ans="",adda="a",sub="ma";

        vector<string> word=split(S);

    

        unordered_map<char,int> res{{'a',1},{'e',1},{'i',1},{'o',1},{'u','1'},{'A',1},{'E',1},{'I',1},{'O',1},{'U',1}};

        

         for(auto w:word)

         {

             if(res[w[0]]!=0)

             {

                  w+=sub;

                 w+=adda;

                 adda+='a';

                 ans+=w+' ';

               

             }else{

                  w+=w[0];

                 w.erase(0,1);

                 w+=sub;

                 w+=adda;

                 adda+='a';

                 ans+=w+' ';

             }

         }

        ans.pop_back();

        return ans;

    }

};

-----------------------------------------------------------------------------------------------------------------------------

class Solution {

public:

    bool is_vowel(char ch){

      ch = tolower(ch);

      if(ch =='a' || ch =='e' || ch =='i' || ch =='o' || ch =='u')

        return true;

     return false;

    }

    

    string toGoatLatin(string str) {

      stringstream ss(str);

      string temp, succ = "maa", ans = "";

      while(!ss.eof()){

        ss >> temp;

        if(!is_vowel(temp[0])){

         temp.push_back(temp[0]);

         temp.erase(temp.begin());

      }

      temp += succ;

      succ.push_back('a');

      ans += (temp + ' ');

     }     

     return ans.substr(0,ans.size() - 1);

   }

};