Skip to content

Commit 9ddb23d

Browse files
committed
feat: add solutions to lc problem: No.0824
No.0824.Goat Latin
1 parent edfcb80 commit 9ddb23d

File tree

4 files changed

+99
-31
lines changed

4 files changed

+99
-31
lines changed

solution/0800-0899/0824.Goat Latin/README.md

+33-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
<li><code>1 &lt;= S.length &lt;= 150</code>。</li>
4747
</ul>
4848

49-
5049
## 解法
5150

5251
<!-- 这里可写通用的实现逻辑 -->
@@ -58,15 +57,46 @@
5857
<!-- 这里可写当前语言的特殊实现逻辑 -->
5958

6059
```python
61-
60+
class Solution:
61+
def toGoatLatin(self, sentence: str) -> str:
62+
ans = []
63+
for i, word in enumerate(sentence.split()):
64+
if word.lower()[0] not in ['a', 'e', 'i', 'o', 'u']:
65+
word = word[1:] + word[0]
66+
word += 'ma'
67+
word += 'a' * (i + 1)
68+
ans.append(word)
69+
return ' '.join(ans)
6270
```
6371

6472
### **Java**
6573

6674
<!-- 这里可写当前语言的特殊实现逻辑 -->
6775

6876
```java
69-
77+
class Solution {
78+
public String toGoatLatin(String sentence) {
79+
List<String> ans = new ArrayList<>();
80+
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
81+
int i = 1;
82+
for (String word : sentence.split(" ")) {
83+
StringBuilder t = new StringBuilder();
84+
if (!vowels.contains(word.charAt(0))) {
85+
t.append(word.substring(1));
86+
t.append(word.charAt(0));
87+
} else {
88+
t.append(word);
89+
}
90+
t.append("ma");
91+
for (int j = 0; j < i; ++j) {
92+
t.append("a");
93+
}
94+
++i;
95+
ans.add(t.toString());
96+
}
97+
return String.join(" ", ans);
98+
}
99+
}
70100
```
71101

72102
### **...**

solution/0800-0899/0824.Goat Latin/README_EN.md

+33-28
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@
66

77
<p>A sentence <code>S</code> is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.</p>
88

9-
10-
119
<p>We would like to convert the sentence to &quot;<em>Goat Latin&quot;</em>&nbsp;(a made-up language similar to Pig Latin.)</p>
1210

13-
14-
1511
<p>The rules of Goat Latin are as follows:</p>
1612

17-
18-
1913
<ul>
2014
<li>If a word begins with a vowel (a, e, i, o, or u), append <code>&quot;ma&quot;</code>&nbsp;to the end of the word.<br />
2115
For example, the word &#39;apple&#39; becomes &#39;applema&#39;.<br />
@@ -27,20 +21,12 @@
2721
For example,&nbsp;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>
2822
</ul>
2923

30-
31-
3224
<p>Return the&nbsp;final sentence representing the conversion from <code>S</code>&nbsp;to Goat&nbsp;Latin.&nbsp;</p>
3325

34-
35-
3626
<p>&nbsp;</p>
3727

38-
39-
4028
<p><strong>Example 1:</strong></p>
4129

42-
43-
4430
<pre>
4531

4632
<strong>Input: </strong>&quot;I speak Goat Latin&quot;
@@ -49,12 +35,8 @@
4935

5036
</pre>
5137

52-
53-
5438
<p><strong>Example 2:</strong></p>
5539

56-
57-
5840
<pre>
5941

6042
<strong>Input: </strong>&quot;The quick brown fox jumped over the lazy dog&quot;
@@ -63,37 +45,60 @@
6345

6446
</pre>
6547

66-
67-
6848
<p>&nbsp;</p>
6949

70-
71-
7250
<p>Notes:</p>
7351

74-
75-
7652
<ul>
7753
<li><code>S</code> contains only uppercase, lowercase and spaces.&nbsp;Exactly one space between each word.</li>
7854
<li><code>1 &lt;= S.length &lt;= 150</code>.</li>
7955
</ul>
8056

81-
82-
8357
## Solutions
8458

8559
<!-- tabs:start -->
8660

8761
### **Python3**
8862

8963
```python
90-
64+
class Solution:
65+
def toGoatLatin(self, sentence: str) -> str:
66+
ans = []
67+
for i, word in enumerate(sentence.split()):
68+
if word.lower()[0] not in ['a', 'e', 'i', 'o', 'u']:
69+
word = word[1:] + word[0]
70+
word += 'ma'
71+
word += 'a' * (i + 1)
72+
ans.append(word)
73+
return ' '.join(ans)
9174
```
9275

9376
### **Java**
9477

9578
```java
96-
79+
class Solution {
80+
public String toGoatLatin(String sentence) {
81+
List<String> ans = new ArrayList<>();
82+
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
83+
int i = 1;
84+
for (String word : sentence.split(" ")) {
85+
StringBuilder t = new StringBuilder();
86+
if (!vowels.contains(word.charAt(0))) {
87+
t.append(word.substring(1));
88+
t.append(word.charAt(0));
89+
} else {
90+
t.append(word);
91+
}
92+
t.append("ma");
93+
for (int j = 0; j < i; ++j) {
94+
t.append("a");
95+
}
96+
++i;
97+
ans.add(t.toString());
98+
}
99+
return String.join(" ", ans);
100+
}
101+
}
97102
```
98103

99104
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String toGoatLatin(String sentence) {
3+
List<String> ans = new ArrayList<>();
4+
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
5+
int i = 1;
6+
for (String word : sentence.split(" ")) {
7+
StringBuilder t = new StringBuilder();
8+
if (!vowels.contains(word.charAt(0))) {
9+
t.append(word.substring(1));
10+
t.append(word.charAt(0));
11+
} else {
12+
t.append(word);
13+
}
14+
t.append("ma");
15+
for (int j = 0; j < i; ++j) {
16+
t.append("a");
17+
}
18+
++i;
19+
ans.add(t.toString());
20+
}
21+
return String.join(" ", ans);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def toGoatLatin(self, sentence: str) -> str:
3+
ans = []
4+
for i, word in enumerate(sentence.split()):
5+
if word.lower()[0] not in ['a', 'e', 'i', 'o', 'u']:
6+
word = word[1:] + word[0]
7+
word += 'ma'
8+
word += 'a' * (i + 1)
9+
ans.append(word)
10+
return ' '.join(ans)

0 commit comments

Comments
 (0)