Goat Latin (LeetCode 824): Solution and Explanation

Easy
String

Convert each word of a sentence: words starting with a vowel get "ma", words starting with a consonant move their first letter to the end before getting "ma", and word number k then gets k extra 'a' letters. "I speak Goat Latin" becomes "Imaa peaksmaaa oatGmaaaa atinLmaaaaa".

Intuition

Every word is converted independently. Its new form depends only on its own first letter and on its position in the sentence.

The vowel test has to accept uppercase vowels too, since "I" and "Apple" count as vowel words.

The trailing 'a' letters grow by one per word, so word i (0-based) receives i + 1 of them.

Approach: Convert word by word

  1. Split sentence on spaces.
  2. For word i: if its first letter is in aeiouAEIOU, take the word as is; otherwise take word[1:] + word[0].
  3. Append "ma" and then i + 1 copies of 'a'.
  4. Join the converted words with single spaces and return the result.

Step-by-step example

sentence = "I speak Goat Latin"
  1. Word 1 I starts with a vowel: Ima, plus one a gives Imaa.
  2. Word 2 speak starts with s: rotate to peaks, add ma, plus two a gives peaksmaaa.
  3. Word 3 Goat starts with G: rotate to oatG, add ma and three a: oatGmaaaa.
  4. Word 4 Latin starts with L: rotate to atinL, add ma and four a: atinLmaaaaa.
  5. Join with spaces: Imaa peaksmaaa oatGmaaaa atinLmaaaaa.

Code: Python, Java, C++

1class Solution:
2    def toGoatLatin(self, sentence: str) -> str:
3        vowels = set("aeiouAEIOU")
4        out = []
5        for i, word in enumerate(sentence.split()):
6            if word[0] in vowels:
7                goat = word + "ma"
8            else:
9                goat = word[1:] + word[0] + "ma"
10            out.append(goat + "a" * (i + 1))
11        return " ".join(out)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n + k^2)

Each letter is copied once, and the 'a' tails add 1 + 2 + ... + k characters for k words.

Space

O(n + k^2)

The output string holds the original letters plus every tail.

Other approaches

Single builder with a running suffix (time O(n + k^2), space O(n + k^2))

Keep a string tail that gains one 'a' per word and append word part + "ma" + tail to one builder. Same cost, slightly less allocation.

Character scan without split (time O(n + k^2), space O(n + k^2))

Walk the sentence once, detecting word starts and ends by spaces, and write the converted word directly. Useful when splitting is awkward in the language.

Common mistakes

  • Checking only lowercase vowels. "I" and "Each" start with uppercase vowels and must keep their order.
  • Starting the 'a' count at 0 instead of 1. The first word already gets one 'a'.
  • Moving the first letter to the end of the vowel words too, or adding "ma" before the rotation.

Frequently asked questions

Does the case of the moved letter change?

No. "Goat" becomes "oatGma..." with the capital G moved to the end unchanged.

Why is the complexity quadratic in the number of words?

Word k gets k extra letters, so the tails add up to k(k + 1) / 2 characters. With at most 150 characters there are at most 75 words, so this is tiny in practice.

Is y a vowel here?

No. Only a, e, i, o and u (in either case) count as vowels.