Article From:

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"

Example 2:

Input: "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"



  • S contains only uppercase, lowercase and spaces. Exactly one space between each word.


Their own solution:

It is to make a single word for each character of the string, to get a single word, and then to make separate words on the words and consonant words that contain vowels, though it is done, but the effect is bad and heavy, and see that others use a lot of string existing methods, conTains (), charAt () I also use it, but like trim () split () substring () did not think that is that I will not think about it, but see the way of others, wow! Good ingenious ~The feeling of ~.

It seems that the brush is less than the brush.

class Solution {
    public String toGoatLatin(String S) {
        if(S == null || S.length() == 0) return "a";
        StringBuilder str = new StringBuilder();
        int i = 0;
        String s = "ma";
        while(i < S.length()){
            s += "a";
            boolean flag = false;
            if(S.charAt(i) == 'a' || S.charAt(i) == 'e' 
               || S.charAt(i) == 'i' || S.charAt(i) == 'o' 
               ||S.charAt(i) == 'u' || S.charAt(i) == 'A' || S.charAt(i) == 'E' 
               || S.charAt(i) == 'I' || S.charAt(i) == 'O' 
               ||S.charAt(i) == 'U')
                flag = true;
                while(i < S.length() && ' ' != S.charAt(i)){
            } else{
                char ch = S.charAt(i++);
                while(i < S.length() && ' ' != S.charAt(i)){
            if(i != S.length()) str.append(' ');
        return str.toString();


Other people’s solution: put it first, wait for a while to do it again, and try to use a good method (at least not so stupid).

class Solution {
    public String toGoatLatin(String S) {
        String vowel = "aeiouAEIOU";
        String result="";
        String addA = "a";
        for (String word:S.split(" ")){
            if (vowel.contains(word.charAt(0)+"")){
                result+=word+"ma"+addA+" ";
            else {
                result+=word.substring(1,word.length())+word.charAt(0)+"ma"+addA+" ";
        return result.trim();



Link of this Article: *LeetCode–Goat Latin

Leave a Reply

Your email address will not be published. Required fields are marked *