Skip to content

Commit e65d5c9

Browse files
committed
solve problem Goat Latin
1 parent e56a924 commit e65d5c9

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ All solutions will be accepted!
102102
|598|[Range Addition II](https://leetcode-cn.com/problems/range-addition-ii/description/)|[java/py/js](./algorithms/RangeAdditionII)|Easy|
103103
|492|[Construct The Rectangle](https://leetcode-cn.com/problems/construct-the-rectangle/description/)|[java/py/js]|(./algorithms/ConstructTheRectangle)|Easy|
104104
|447|[Number Of Boomerangs](https://leetcode-cn.com/problems/number-of-boomerangs/description/)|[java/py/js](./algorithms/NumberOfBoomerangs)|Easy|
105+
|824|[Goat Latin](https://leetcode-cn.com/problems/goat-latin/description/)|[java/py/js](./algorithms/GoatLatin)|Easy|
105106

106107
# Database
107108
|#|Title|Solution|Difficulty|

algorithms/GoatLatin/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Goat Latin
2+
This problem is easy to solve

algorithms/GoatLatin/Solution.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public String toGoatLatin(String S) {
3+
String vowels = "aeiouAEIOU";
4+
String[] words = S.split(" ");
5+
String res = "";
6+
7+
for (int i = 0; i < words.length; i++) {
8+
String word = words[i];
9+
10+
if (res.length() != 0) {
11+
res += " ";
12+
}
13+
14+
if (!vowels.contains(word.substring(0, 1))) {
15+
word = word.substring(1) + word.substring(0, 1);
16+
}
17+
18+
word += "ma";
19+
for (int j = 0; j <= i; j++) {
20+
word += "a";
21+
}
22+
23+
res += word;
24+
}
25+
return res;
26+
}
27+
}

algorithms/GoatLatin/solution.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} S
3+
* @return {string}
4+
*/
5+
var toGoatLatin = function(S) {
6+
let vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'],
7+
words = S.split(' '),
8+
res = ''
9+
10+
for (let i = 0; i < words.length; i++) {
11+
let word = words[i]
12+
if (res.length != 0) {
13+
res += ' '
14+
}
15+
16+
if (vowels.findIndex(vowel => word[0] === vowel) === -1) {
17+
word = word.substr(1) + word[0]
18+
}
19+
word += 'ma'
20+
for (let j = 0; j <= i; j++) {
21+
word += 'a'
22+
}
23+
res += word
24+
}
25+
return res
26+
};

algorithms/GoatLatin/solution.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def toGoatLatin(self, S):
3+
"""
4+
:type S: str
5+
:rtype: str
6+
"""
7+
res = ''
8+
9+
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
10+
words = S.split(' ')
11+
for i in range(len(words)):
12+
word = words[i]
13+
14+
if len(res) != 0:
15+
res += ' '
16+
17+
if word[0] not in vowels:
18+
word = word[1:] + word[0]
19+
word += 'ma'
20+
word += 'a' * (i + 1)
21+
res += word
22+
23+
return res

0 commit comments

Comments
 (0)