题目地址:https://leetcode.com/problems/goat-latin/description/

题目描述

Asentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.

Wewould like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)

Therules 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 S to Goat Latin.

Example 1:

Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

Example 2:

Input: "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

Notes:

1、 Scontainsonlyuppercase,lowercaseandspaces.Exactlyonespacebetweeneachword.;
2、 1<=S.length<=150.;

题目大意

要把英语翻译成Goat Latin语。翻译规则如下:

把英文句子按照空格分割成各个单词。

1、 如果单词以元音字母开头,后面接上"ma"
2、 如果不以元音开头,把首字母移到最后,然后接上"ma"
3、 每个单词后面接上在句子中出现的第几位置个"a"

解题方法

python天生的适合处理字符串问题。这种纯粹的拼操作的,没有思考的题目,分分钟搞定啊~

太简单了,不讲了。

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)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发