Watch 10 video solutions for Goat Latin, a easy level problem involving String. This walkthrough by Nick White has 3,960 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string sentence that consist 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:
'a', 'e', 'i', 'o', or 'u'), append "ma" to the end of the word.
"apple" becomes "applema"."ma".
"goat" becomes "oatgma".'a' to the end of each word per its word index in the sentence, starting with 1.
"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.
Example 1:
Input: sentence = "I speak Goat Latin" Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Example 2:
Input: sentence = "The quick brown fox jumped over the lazy dog" Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
Constraints:
1 <= sentence.length <= 150sentence consists of English letters and spaces.sentence has no leading or trailing spaces.sentence are separated by a single space.Problem Overview: You are given a sentence and must convert every word into "Goat Latin" following a set of transformation rules. Words starting with vowels keep their order, while words starting with consonants move the first letter to the end. Each word also gets "ma" plus an increasing number of 'a' characters based on its position.
Approach 1: String Manipulation with Direct Index Modifications (O(n) time, O(n) space)
Split the sentence into individual words, then process them sequentially. For each word, check the first character against a vowel set (a, e, i, o, u in both cases). If the first letter is a consonant, move it to the end using substring or slicing operations. Append "ma" and a sequence of 'a' characters equal to the word's index (1-based). Join the processed words back into a sentence. This approach relies entirely on basic string operations and iteration, making it straightforward and easy to implement in languages like Python or JavaScript.
Approach 2: Using StringBuilder for Efficient String Manipulation (O(n) time, O(n) space)
Languages like Java and C# benefit from StringBuilder because repeated string concatenation can create many intermediate strings. Iterate through the words after splitting the sentence, then build each transformed word using a StringBuilder. Append characters conditionally depending on whether the word starts with a vowel or consonant, followed by "ma" and the appropriate number of 'a' characters. Using a builder avoids repeated allocations and keeps the transformation linear relative to the total input size. The logic is still pure string processing, but with more efficient memory handling.
Both approaches follow the same transformation logic. The difference lies in how strings are constructed. Direct concatenation works well in scripting languages where string operations are optimized, while builder-based approaches are preferred in strongly typed languages with immutable string objects.
Recommended for interviews: The direct string manipulation approach is usually expected first. It clearly demonstrates that you can parse sentences, handle character checks, and apply transformation rules step by step. If coding in Java or C#, interviewers often expect the StringBuilder variation to avoid inefficient concatenation. The key skill being tested is practical string manipulation and careful rule implementation rather than complex algorithms.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Manipulation with Direct Index Modifications | O(n) | O(n) | Best for Python or JavaScript where string slicing and concatenation are efficient and code readability matters. |
| Using StringBuilder for Efficient String Manipulation | O(n) | O(n) | Preferred in Java or C# to avoid repeated string allocations during concatenation. |