> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Goat Latin Python Solution with Tests

> Tested Python solution for LeetCode 824 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 824, [Easy](/catalog/easy). Topics: [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/goat-latin/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 824   # by problem number
lcpy gen -s goat_latin   # by problem name
```

## Problem

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:

* If a word begins with a vowel (`'a'`, `'e'`, `'i'`, `'o'`, or `'u'`), append `"ma"` to the end of the word.
  * For example, the word `"apple"` becomes `"applema"`.
* If a word begins with a consonant (i.e., not a vowel), remove the first letter and append it to the end, then add `"ma"`.
  * For example, the word `"goat"` becomes `"oatgma"`.
* Add one letter `'a'` to the end of each word per its word index in the sentence, starting with `1`.
  * For example, the first word gets `"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.

### Examples

```
Input: sentence = "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
```

```
Input: sentence = "apple"
Output: "applema"
```

```
Input: sentence = "goat"
Output: "oatgma"
```

### Constraints

* 1 \<= sentence.length \<= 150
* sentence consists of English letters and spaces.
* sentence has no leading or trailing spaces.
* All the words in sentence are separated by a single space.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/goat_latin/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/goat_latin/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2) where n is the length of the sentence (appending index-sized 'a' runs)
    # Space: O(n^2) for the output sentence
    def to_goat_latin(self, sentence: str) -> str:
        vowels = frozenset("aeiouAEIOU")
        words = sentence.split(" ")
        converted = []
        for i, word in enumerate(words, start=1):
            stem = word if word[0] in vowels else word[1:] + word[0]
            converted.append(f"{stem}ma{'a' * i}")
        return " ".join(converted)
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) where n is the length of the sentence (appending index-sized 'a' runs) | O(n^2) for the output sentence |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.