09 Sept 2026Go / Python / TypeScriptEasy

Goat Latin

Transform every word in a sentence according to the Goat Latin rules.

Split the sentence, move an initial consonant when needed, append the fixed suffix, then add one more 'a' per word position.

complexity

O(n) time and O(n) output space.

solution files

  • Go goat-latin/solution.go
  • Python goat-latin/solution.py
  • TypeScript goat-latin/solution.ts

Solution files

Gogoat-latin/solution.go
package main

import "strings"

func toGoatLatin(sentence string) string {
	vowels := "aeiouAEIOU"
	words := strings.Fields(sentence)
	for index, word := range words {
		if !strings.ContainsRune(vowels, rune(word[0])) {
			word = word[1:] + word[:1]
		}
		words[index] = word + "ma" + strings.Repeat("a", index+1)
	}
	return strings.Join(words, " ")
}
Pythongoat-latin/solution.py
class Solution:
    def toGoatLatin(self, sentence: str) -> str:
        vowels = set(class="syntax-string">"aeiouAEIOU")
        result: list[str] = []
        for index, word in enumerate(sentence.split(), class="syntax-number">1):
            base = word if word[class="syntax-number">0] in vowels else word[class="syntax-number">1:] + word[class="syntax-number">0]
            result.append(base + class="syntax-string">"ma" + class="syntax-string">"a" * index)
        return class="syntax-string">" ".join(result)
TypeScriptgoat-latin/solution.ts
function toGoatLatin(sentence: string): string {
  const vowels = new Set(class="syntax-string">"aeiouAEIOU");
  return sentence.split(class="syntax-string">" ").map((word, index) => {
    const base = vowels.has(word[class="syntax-number">0]) ? word : word.slice(class="syntax-number">1) + word[class="syntax-number">0];
    return base + class="syntax-string">"ma" + class="syntax-string">"a".repeat(index + class="syntax-number">1);
  }).join(class="syntax-string">" ");
}