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 <= 150
sentence
consists of English letters and spaces.sentence
has no leading or trailing spaces.sentence
are separated by a single space.This problem involves transforming a sentence into "Goat Latin" according to specific rules. The solution involves iterating through each word, applying transformations based on whether it starts with a vowel or consonant, and then adding 'a's based on the word's position.
Algorithm:
Split the sentence into words: The input sentence is first split into individual words using space as a delimiter.
Iterate through each word: The code iterates through each word in the sentence.
Vowel or Consonant Check: For each word:
Append 'a's: The number of 'a's appended to each word corresponds to its index (starting from 1). So the first word gets one 'a', the second gets two, and so on.
Join the words: Finally, the modified words are joined back together with spaces to form the Goat Latin sentence.
Time Complexity Analysis:
Therefore, the overall time complexity is dominated by the splitting and joining steps, resulting in O(n + m*k). In most cases, this can be considered approximately linear, O(n), because m (number of words) is usually proportional to n (sentence length) and k (average word length) is relatively small and constant.
Space Complexity Analysis:
The space complexity depends on the size of the intermediate data structures:
Thus, the overall space complexity is O(n), which is linear in the input sentence length.
Code Examples with Explanations:
The code examples in Python, Java, TypeScript, and Rust all follow the same basic algorithm described above. Each language has its own syntax, but the core logic remains consistent. Key points to observe are:
/[aeiou]/i
in TypeScript) or a set of vowels for efficient checking.substring
in Java, slice
in TypeScript) for moving characters around.+=
in Python, append
in Java's StringBuilder
).The choice of which language to use depends on personal preference and the context of the problem. All four languages presented provide clear and efficient solutions to the Goat Latin problem.