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:
- If the word begins with a vowel (
a,e,i,o,u, in either case), append"ma". - Otherwise move the first letter to the end and then append
"ma". - Append one
"a"for the first word, two for the second, and so on — thei-th word (1-indexed) getsicopies 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 <= 150sentence consists of English letters and single spaces.There are no leading or trailing spaces.
Hints for Goat Latin
Hint 1
Split on spaces and handle each word independently.
Hint 2
The vowel test must cover both cases —
"I"is a vowel word.Hint 3
The
i-th word (1-indexed) ends with"ma"followed byilettersa.
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
- Split the sentence on single spaces.
- For each word at index
i(0-based): if the first character is not a vowel, rotate it to the end. - Append
"ma"followed byi + 1copies of"a". - 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