LC 824. Goat Latin

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
private:
    vector<string> removeDupWord(string str) 
    { 
        vector<string>ans;
        string word = ""; 
        for (auto x : str) 
        { 
            if (x == ' ') 
            { 
                ans.push_back(word); 
                word = ""; 
            } 
            else
            { 
                word = word + x; 
            } 
    }  
        ans.push_back(word);
        return ans;
    }
public:
    string toGoatLatin(string S) {
        string result = "";
        vector<string>str = removeDupWord(S);
        vector<char>vowel = {'a','A','e','E','I','i','o','O','U','u'};
        for(int i=0;i<str.size();i++){
            int target = i+1;
            if(find (vowel.begin(), vowel.end(), str[i][0])==vowel.end()){
                char first = str[i][0];
                for(int j=0;j<str[i].size()-1;j++){
                    str[i][j] = str[i][j+1];
                }
                str[i][str[i].size()-1] = first;
            }
            str[i] += "ma";
            while(target--){
                str[i].push_back('a');
            }
            str[i] += " ";
            result += str[i];
        }
        result.pop_back();
        return result;
    }
};


Dicussion Forum