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.
Converting a sentence to Goat Latin involves directly modifying each word according to rules based on their starting letter (vowel or consonant) and their position in the sentence list. We iterate over each word in the sentence, apply the specific transformations, and finally reconstruct the sentence.
The Python solution uses a straightforward approach: it first checks if the word starts with a vowel. If it does, 'ma' is appended. If not, the first letter is moved to the end before appending 'ma'. Afterwards, 'a' characters are appended based on the word's position index. Each word is then gathered into a list and joined into the final sentence.
Python
JavaScript
Time Complexity: O(n) where n is the number of characters in the sentence, because each character is processed once.
Space Complexity: O(n) required for the words list.
This approach leverages StringBuilder (in languages where it is available) to efficiently build the result string, managing memory and time complexity by reducing intermediate operations.
In the Java solution, a StringBuilder is used to construct each word and the final sentence efficiently. For each word, we use character checks on the first character to determine consonant handling or vowel treatment and then build each word part by part.
Time Complexity: O(n) due to single-pass string operations and constructions.
Space Complexity: O(n) for storing transitory character builds and the final sentence.
Python
Java
TypeScript
Rust
| Approach | Complexity |
|---|---|
| String Manipulation with Direct Index Modifications | Time Complexity: O(n) where n is the number of characters in the sentence, because each character is processed once. |
| Using StringBuilder for Efficient String Manipulation | Time Complexity: O(n) due to single-pass string operations and constructions. |
| Default Approach | — |
| 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. |
LeetCode Goat Latin Solution Explained - Java • Nick White • 3,960 views views
Watch 9 more video solutions →Practice Goat Latin with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor