Goat Latin

Easy
512.

Goat Latin

Goat Latin is an easy coding problem on strings, asked in Amazon and Adobe interviews.

Convert a sentence to Goat Latin, a made-up language with these rules applied to each word:

  1. If the word begins with a vowel (a, e, i, o, u, in either case), append "ma".
  2. Otherwise move the first letter to the end and then append "ma".
  3. Append one "a" for the first word, two for the second, and so on — the i-th word (1-indexed) gets i copies of "a".

Words are separated by single spaces. Return the converted sentence.

Example 1

Input
s = "I speak Goat Latin"
Output
Imaa peaksmaaa oatGmaaaa atinLmaaaaa

Example 2

Input
s = "The quick brown fox"
Output
heTmaa uickqmaaa rownbmaaaa oxfmaaaaa

Example 3

Input
s = "a"
Output
amaa

Constraints

  • 1 <= sentence.length <= 150
  • sentence consists of English letters and single spaces.
  • There are no leading or trailing spaces.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} sentence
 * @return {string}
 */
var toGoatLatin = function(sentence) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Goat Latin — Solution in Python, Java & C++

Goat Latin solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem, asked at Amazon and Adobe.

Goat Latin is an easy coding problem on strings, asked in Amazon and Adobe interviews.

  • Difficulty: Easy
  • Topics: Strings
  • Asked at: Amazon, Adobe
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Convert a sentence to Goat Latin, a made-up language with these rules applied to each word:

  1. If the word begins with a vowel (a, e, i, o, u, in either case), append "ma".
  2. Otherwise move the first letter to the end and then append "ma".
  3. Append one "a" for the first word, two for the second, and so on — the i-th word (1-indexed) gets i copies of "a".

Words are separated by single spaces. Return the converted sentence.

Example 1

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

Example 2

Input: s = "The quick brown fox"
Output: heTmaa uickqmaaa rownbmaaaa oxfmaaaaa

Example 3

Input: s = "a"
Output: amaa

Constraints

  • 1 <= sentence.length <= 150
  • sentence consists of English letters and single spaces.
  • There are no leading or trailing spaces.

Hints for Goat Latin

  1. Hint 1

    Split on spaces and handle each word independently.

  2. Hint 2

    The vowel test must cover both cases — "I" is a vowel word.

  3. Hint 3

    The i-th word (1-indexed) ends with "ma" followed by i letters a.

How to solve Goat Latin

Every rule is local to one word plus its position, so a single split-transform-join does the whole job.

Approach

  1. Split the sentence on single spaces.
  2. For each word at index i (0-based): if the first character is not a vowel, rotate it to the end.
  3. Append "ma" followed by i + 1 copies of "a".
  4. Join the transformed words back with single spaces.

Why it works

The rules never look outside a word other than to read its index, so the transformation is a plain map over the word list.

Complexity

  • Time — O(n²) in the worst case, because the suffix grows with the word index
  • Space — O(n²) for the same reason

Pitfalls

  • Testing only lowercase vowels mishandles a capitalised word like "I" or "Apple".
  • The suffix count is 1-based — the first word gets "maa", not "ma".
  • Rebuilding with join(" ") matters; adding a trailing space fails the exact judge.

Reference solution

Python

def toGoatLatin(sentence: str) -> str:
    vowels = "aeiouAEIOU"
    out = []
    for i, w in enumerate(sentence.split(" ")):
        if w[0] not in vowels:
            w = w[1:] + w[0]
        out.append(w + "ma" + "a" * (i + 1))
    return " ".join(out)

Java

public static String toGoatLatin(String sentence) {
    String vowels = "aeiouAEIOU";
    String[] words = sentence.split(" ");
    StringBuilder res = new StringBuilder();
    for (int i = 0; i < words.length; i++) {
        String w = words[i];
        if (vowels.indexOf(w.charAt(0)) < 0) w = w.substring(1) + w.charAt(0);
        if (i > 0) res.append(' ');
        res.append(w).append("ma");
        for (int k = 0; k <= i; k++) res.append('a');
    }
    return res.toString();
}

C++

string toGoatLatin(string sentence) {
    string vowels = "aeiouAEIOU";
    vector<string> words;
    string cur;
    for (char c : sentence) {
        if (c == ' ') { words.push_back(cur); cur.clear(); }
        else cur += c;
    }
    words.push_back(cur);
    string res;
    for (int i = 0; i < (int) words.size(); i++) {
        string w = words[i];
        if (vowels.find(w[0]) == string::npos) w = w.substr(1) + w[0];
        if (i > 0) res += ' ';
        res += w + "ma" + string(i + 1, 'a');
    }
    return res;
}

JavaScript

var toGoatLatin = function(sentence) {
    const vowels = "aeiouAEIOU";
    const words = sentence.split(" ");
    const out = [];
    for (let i = 0; i < words.length; i++) {
        let w = words[i];
        if (vowels.indexOf(w.charAt(0)) === -1) w = w.slice(1) + w.charAt(0);
        let tail = "";
        for (let k = 0; k <= i; k++) tail += "a";
        out.push(w + "ma" + tail);
    }
    return out.join(" ");
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings