Skip to content

Commit 9489dc6

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.8 MB (39.23%)
1 parent b88431e commit 9489dc6

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

0851-goat-latin/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>You are given a string <code>sentence</code> that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.</p>
2+
3+
<p>We would like to convert the sentence to &quot;Goat Latin&quot; (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:</p>
4+
5+
<ul>
6+
<li>If a word begins with a vowel (<code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, or <code>&#39;u&#39;</code>), append <code>&quot;ma&quot;</code> to the end of the word.
7+
8+
<ul>
9+
<li>For example, the word <code>&quot;apple&quot;</code> becomes <code>&quot;applema&quot;</code>.</li>
10+
</ul>
11+
</li>
12+
<li>If a word begins with a consonant (i.e., not a vowel), remove the first letter and append it to the end, then add <code>&quot;ma&quot;</code>.
13+
<ul>
14+
<li>For example, the word <code>&quot;goat&quot;</code> becomes <code>&quot;oatgma&quot;</code>.</li>
15+
</ul>
16+
</li>
17+
<li>Add one letter <code>&#39;a&#39;</code> to the end of each word per its word index in the sentence, starting with <code>1</code>.
18+
<ul>
19+
<li>For example, the first word gets <code>&quot;a&quot;</code> added to the end, the second word gets <code>&quot;aa&quot;</code> added to the end, and so on.</li>
20+
</ul>
21+
</li>
22+
</ul>
23+
24+
<p>Return<em> the final sentence representing the conversion from sentence to Goat Latin</em>.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong class="example">Example 1:</strong></p>
28+
<pre><strong>Input:</strong> sentence = "I speak Goat Latin"
29+
<strong>Output:</strong> "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
30+
</pre><p><strong class="example">Example 2:</strong></p>
31+
<pre><strong>Input:</strong> sentence = "The quick brown fox jumped over the lazy dog"
32+
<strong>Output:</strong> "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
33+
</pre>
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= sentence.length &lt;= 150</code></li>
39+
<li><code>sentence</code> consists of English letters and spaces.</li>
40+
<li><code>sentence</code> has no leading or trailing spaces.</li>
41+
<li>All the words in <code>sentence</code> are separated by a single space.</li>
42+
</ul>

0851-goat-latin/solution.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Approach 1: Just Strings
2+
3+
class Solution:
4+
def toGoatLatin(self, sentence: str) -> str:
5+
def process(idx, word):
6+
if word[0] not in 'aeiouAEIOU':
7+
word = word[1:] + word[0]
8+
return word + 'ma' + ('a' * idx)
9+
10+
return ' '.join([process(i, w) for i, w in enumerate(sentence.split(), start = 1)])
11+

0 commit comments

Comments
 (0)