-
Notifications
You must be signed in to change notification settings - Fork 43
/
goat-latin.py
139 lines (120 loc) · 4.2 KB
/
goat-latin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
824. Goat Latin
Easy
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.
Example 1:
Input: sentence = "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Example 2:
Input: sentence = "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
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
"""
# V0
class Solution:
def toGoatLatin(self, S):
"""
:type S: str
:rtype: str
"""
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
words = S.split(' ')
new_words = []
for i, word in enumerate(words):
if word[0] in vowels:
word += 'ma'
else:
word = word[1:] + word[0] + 'ma'
word += 'a' * (i + 1)
new_words.append(word)
return ' '.join(new_words)
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/80471925
class Solution:
def toGoatLatin(self, S):
"""
:type S: str
:rtype: str
"""
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
words = S.split(' ')
new_words = []
for i, word in enumerate(words):
if word[0] in vowels:
word += 'ma'
else:
word = word[1:] + word[0] + 'ma'
word += 'a' * (i + 1)
new_words.append(word)
return ' '.join(new_words)
# V1'
# http://bookshadow.com/weblog/2018/04/29/leetcode-goat-latin/
class Solution(object):
def toGoatLatin(self, S):
"""
:type S: str
:rtype: str
"""
ans = []
for idx, word in enumerate(S.split()):
latin = word
if word[0].lower() not in 'aeiou':
latin = word[1:] + word[0]
latin += 'ma' + 'a' * (idx + 1)
ans.append(latin)
return ' '.join(ans)
# V1''
# https://www.jiuzhang.com/solution/1394-goat-latin/#tag-other-lang-python
class Solution:
"""
@param S:
@return: nothing
"""
def toGoatLatin(self, S):
if not S:
return ''
start, end = 0, 0
word_count = 0
answer = ''
vowels = ['a','e','i','o','u', 'A', 'E', 'I', 'O', 'U']
while start < len(S):
while end < len(S) and S[end] != ' ':
end += 1
word_count += 1
word = S[start:end]
if word[0] in vowels:
word += 'ma'
else:
word = word[1:] + word[0] + 'ma'
postfix = ''
for i in range(word_count):
postfix += 'a'
answer+= word + postfix + ' '
start, end = end + 1, end + 2
return answer[:-1]
# V2
class Solution(object):
def toGoatLatin(self, S):
"""
:type S: str
:rtype: str
"""
def convert(S):
vowel = set('aeiouAEIOU')
for i, word in enumerate(S.split(), 1):
if word[0] not in vowel:
word = word[1:] + word[:1]
yield word + 'ma' + 'a'*i
return " ".join(convert(S))