Domain 1EasyCORE

Goat Latin

The problem requires converting a given sentence into "Goat Latin," which follows specific rules: 1. If a word starts with a vowel (a, e, i, o, u), append `"ma"` to the end of the word. 2. If a word starts with a consonant, move the first letter to the end and then append `"ma"`. 3. Add one letter `'a'` to the end of each word based on its position in the sentence (1st word gets `"a"`, 2nd word gets `"aa"`, etc.).

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#String

824. Goat Latin

Model: qwen2.5-coder:14b
Topics: String
FAANG asked: 1
Acceptance rate: 66.9%


Problem Breakdown

The problem requires converting a given sentence into “Goat Latin,” which follows specific rules:

  1. If a word starts with a vowel (a, e, i, o, u), append "ma" to the end of the word.
  2. If a word starts with a consonant, move the first letter to the end and then append "ma".
  3. Add one letter 'a' to the end of each word based on its position in the sentence (1st word gets "a", 2nd word gets "aa", etc.).

Inputs:

  • A string S composed of words separated by spaces.

Outputs:

  • A new string representing the sentence converted into Goat Latin.

Implicit Constraints or Edge Cases:

  • The input string contains only uppercase, lowercase letters, and spaces.
  • There is exactly one space between each word.
  • The length of the input string S is between 1 and 150 characters.

Intuition

A brute-force approach would involve iterating through each word in the sentence, applying the Goat Latin rules, and then concatenating the results. However, this approach might not be optimal in terms of time complexity if we don’t consider efficient string manipulation techniques.

The key insight is to break down the problem into smaller steps:

  1. Split the input sentence into individual words.
  2. Apply the Goat Latin transformation rules to each word.
  3. Append the appropriate number of 'a' characters based on the word’s position.
  4. Join the transformed words back into a single string with spaces in between.

Approach

To solve this problem, we can use the following algorithm:

  1. Split the input sentence S into a list of words using the split() method.
  2. Initialize an empty list to store the transformed words.
  3. Iterate through each word in the list:
    • Check if the first character of the word is a vowel (case-insensitive).
    • If it’s a vowel, append "ma" to the end of the word.
    • If it’s a consonant, move the first letter to the end and then append "ma".
    • Append 'a' repeated by the current word index + 1 to the transformed word.
  4. Join the list of transformed words into a single string with spaces in between using the join() method.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the input string S. We iterate through each character of the string once when splitting it into words and again when transforming each word.
  • Space Complexity: O(n), as we store the transformed words in a list and create a new string for the final result.

Solution

def toGoatLatin(S: str) -> str:
    vowels = set('aeiouAEIOU')
    words = S.split()
    goat_latin_words = []

    for i, word in enumerate(words):
        if word[0] in vowels:
            transformed_word = word + "ma"
        else:
            transformed_word = word[1:] + word[0] + "ma"
        
        transformed_word += 'a' * (i + 1)
        goat_latin_words.append(transformed_word)

    return ' '.join(goat_latin_words)

Edge Cases Handled

  • Single Word: The code handles sentences with a single word correctly by appending the appropriate number of 'a' characters.
  • All Vowels/Consonants: The code correctly transforms words starting with all vowels or consonants.
  • Mixed Case Words: The code treats uppercase and lowercase vowels equally, ensuring case-insensitive transformation.