You are given a string sentence that consist 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 sentence to Goat Latin.

&nbsp;
Example 1:
Input: sentence = "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Example 2:
Input: sentence = "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

&nbsp;
Constraints:


	1 &lt;= sentence.length &lt;= 150
	sentence consists of English letters and spaces.
	sentence has no leading or trailing spaces.
	All the words in sentence are separated by a single space.


class Solution:
    def toGoatLatin(self, sentence: str) -> str:
        wordList, result, index = sentence.split(' '), "", 1
        for word in wordList:
            if index > 1:
                result += " "
            firstLetter = word[0]
            if firstLetter in 'aeiouAEIOU':
                result += word + "ma"
            else:
                result += word[1:] + firstLetter + "ma"
            for i in range(index):
                result += 'a'
            index += 1
        return result
class Solution {
    public String toGoatLatin(String sentence) {
        StringBuffer sb = new StringBuffer();
        StringBuffer temp = new StringBuffer("a"); // temporary stringbuffer

        for(String str : sentence.split(" ")) {
            if(beginsWithConsonant(str)) {
                sb.append(str.substring(1)); // removing the first letter
                sb.append(str.charAt(0)); // appending it to the end
            } else {
                sb.append(str);
            }

            sb.append("ma"); // appending "ma" to the end of the word (common operation)
            sb.append(temp); // adding one letter 'a' to the end of each word

            // the first word gets "a" added to the end,
            // the second word gets "aa" added to the end,
            // and so on.
            temp.append("a"); // increasing the a's for every word
            sb.append(" "); // to put space between words
        }

        return sb.toString().trim(); // using trim() to remove the one extra space from the end of string.
    }

    public boolean beginsWithConsonant(String str) {
        return "aeiou".indexOf(str.toLowerCase().charAt(0)) == -1;
    }
}
class Solution {
public:
    string toGoatLatin(string sentence) {
        sentence += ' ';
        vector<string> Words;
        string TEMP, SOL, A = "a";
        for (int i = 0; i < sentence.size(); ++i)
        {
            if (sentence[i] == ' ')
            {
                Words.push_back(TEMP);
                TEMP = "";
            }
            else
                TEMP += sentence[i];
        }
        for (string V : Words)
        {
            char TMP = tolower(V[0]);
            if (TMP == 'a' || TMP == 'e' || TMP == 'i' || TMP == 'o' || TMP == 'u')
                V += "ma";
            else
            {
                TMP = V[0];
                V.erase(0, 1);
                V += TMP;
                V += "ma";
            }
            V += A;
            A += 'a';
            SOL += V + ' ';
        }
        SOL.erase(SOL.size() - 1, 1);
        return SOL;
    }
};
/**
 * @param {string} S
 * @return {string}
 */
var toGoatLatin = function(S) {
    let re = /[aeiou]/gi
    let word = S.split(' ')
    let add = 0
    let arr = []
    
    for(let i= 0; i < word.length; i++) {
        if(!word[i].substring(0,1).match(re)) {
            arr = word[i].split('')
            let letter = arr.shift()
            arr.push(letter)
            word[i] = arr.join('')
        }
        
        // have to append 'ma' regardless if it starts with a vowel
        word[i] += 'ma'
        
        // append 'a' number of times as the index + 1
        add = i+1
        while(add >0) {
            word[i] += 'a'
            add--
        }
    }
    
    return word.join(' ')
};
Goat Latin