1 class Solution(object): 2 def toGoatLatin(self, S): 3 """ 4 :type S: str 5 :rtype: str 6 """ 7 # 定义元音字母集合 8 vowel = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] 9 # 用空格分割S 10 words = S.split() 11 # 返回值 12 res = "" 13 # 遍历S中的所有单词 14 for i, word in enumerate(words): 15 # 每个单词拼接时用空格隔开 16 res += " " 17 if word[0] in vowel: 18 res = res + word + "ma" + 'a' * (i + 1) 19 else: 20 res = res + word[1:] + word[0] + "ma" + 'a' * (i + 1) 21 # 截掉首位的空格 22 return res[1:] 23 24 25 if __name__ == '__main__': 26 solution = Solution() 27 print(solution.toGoatLatin("I speak Goat Latin"))