512. Goat Latin
Your function receives sentence — words made of upper- and lowercase letters, separated by single spaces — and must return its translation into "Goat Latin", a made-up dialect built word by word:
- If a word starts with a vowel (
a,e,i,o,u, in either case), append"ma"to it. - If it starts with a consonant, move that first letter to the end of the word, then append
"ma". - Finally, every word gains a tail of letter
'a's equal to its position in the sentence (1-indexed): the first word gets"a", the second"aa", and so on.
Return the transformed words joined by single spaces.
Example 1:
Input: sentence = "You are super smart"
Output: "ouYmaa aremaaa upersmaaaa martsmaaaaa"
Explanation: "You" starts with a consonant: rotate to "ouY", add "ma", add one 'a' → "ouYmaa". "are" starts with a vowel: "are" + "ma" + "aa" → "aremaaa". Each later word gains one more trailing 'a'.
Example 2:
Input: sentence = "Ask me anything"
Output: "Askmaa emmaaa anythingmaaaa"
Explanation: "Ask" begins with a vowel (capital A counts), so it keeps its shape: "Askma" + "a". "me" rotates to "em", then "ma" + "aa" gives "emmaaa".
Constraints:
- 1 ≤ sentence.length ≤ 150
- sentence consists of English letters and spaces
- sentence has no leading or trailing spaces, and its words are separated by single spaces
Hints:
Handle one word at a time: the vowel test must accept both cases (put "aeiouAEIOU" in a set), and the consonant rule is just slicing — word[1:] + word[0].
The 'a' tail grows by one per word, so carry a suffix string across the loop and extend it each iteration instead of rebuilding it. Collect the transformed words and join once at the end — repeated string concatenation copies the whole result every time.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: sentence = "You are super smart"
Expected output: "ouYmaa aremaaa upersmaaaa martsmaaaaa"