Goat Latin - Problem

You are given a string sentence that consists 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 sentence to Goat Latin.

Input & Output

Example 1 — Basic Mixed Case
$ Input: sentence = "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
💡 Note: Word 1 'I' starts with vowel: I + ma + a = 'Imaa'. Word 2 'speak' starts with consonant: peak + s + ma + aa = 'peaksmaaa'. Word 3 'Goat' starts with consonant: oat + G + ma + aaa = 'oatGmaaaa'. Word 4 'Latin' starts with consonant: atin + L + ma + aaaa = 'atinLmaaaaa'.
Example 2 — All Vowel Start
$ Input: sentence = "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
💡 Note: Each word is transformed: consonant words move first letter to end, all get 'ma' and position-based 'a's added.
Example 3 — Single Character Words
$ Input: sentence = "a I o"
Output: "amaa Imaaa omaaaa"
💡 Note: Single character vowels: 'a' becomes 'amaa', 'I' becomes 'Imaaa', 'o' becomes 'omaaaa' with position-based 'a's.

Constraints

  • 1 ≤ sentence.length ≤ 150
  • sentence consists of English letters and spaces only
  • sentence has no leading or trailing spaces
  • All the words in sentence are separated by a single space

Visualization

Tap to expand
Goat Latin Translation ProcessInput: "I speak Goat Latin"Transformation Rules:• Vowel start: word + "ma" + position-based 'a's• Consonant start: move first letter to end + "ma" + 'a'sIspeakGoatLatinImaapeaksmaaaoatGmaaaaatinLmaaaaaOutput: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Understanding the Visualization
1
Input
Original sentence with words separated by spaces
2
Transform
Apply vowel/consonant rules and add position-based 'a's
3
Output
Final Goat Latin sentence
Key Takeaway
🎯 Key Insight: The transformation depends on the first character being a vowel or consonant, with position-based 'a's added to each word
Asked in
Google 15 Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen