题目要求

A sentence S is given, composed 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 S to Goat Latin.

题目分析及思路

给定一个句子,其中只包含word和space,word只由大小写字母组成。先将这个句子进行转换,转换规则如下:

  1)若一个word以元音字母开头,则在这个word末尾添加'ma'

  2)若一个word以辅音字母开头,则将这个word开头字母移到末尾,并在这个word末尾添加'ma'

  3)对句中的每一个word,根据它的索引在word末尾添加'a'。若索引为0,则添加一个'a',为1则添加两个'a',以此类推

最后将转换好的句子返回。

python代码

class Solution:

def toGoatLatin(self, S: str) -> str:

S = S.split()

s_gls = ''

for idx, s in enumerate(S):

if s[0] in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']:

s_gl = s + 'ma'

else:

s_gl = s[1:] + s[0] + 'ma'

s_gl += (idx+1)*'a'

s_gls += s_gl + ' '

return s_gls.strip()

LeetCode 824 Goat Latin 解题报告的更多相关文章

  1. 【LeetCode】824. Goat Latin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. [LeetCode] 824. Goat Latin

    Description A sentence S is given, composed of words separated by spaces. Each word consists of lowe ...

  3. LeetCode 824. Goat Latin (山羊拉丁文)

    题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...

  4. 824. Goat Latin - LeetCode

    Questioin 824. Goat Latin Solution 题目大意:根据要求翻译句子 思路:转换成单词数组,遍历数组,根据要求转换单词 Java实现: 用Java8的流实现,效率太低 pu ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  7. 【Leetcode_easy】824. Goat Latin

    problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

随机推荐

  1. PHP可变参数

    0x00 缘起 在laravel的源码里经常可以看到下面的函数形式 $func(...$args) 0x01 可变参数旧写法 这表示$func支持可变参数,在php5.6之前则是在函数体内调用 fun ...

  2. 30. Substring with Concatenation of All Words

    题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  3. hadoop无法停止

    停止hadoop集群,运行命令 $ sh stop-all.sh 出现提示: no resourcemanager to stop host10: no nodemanager to stop hos ...

  4. Java知多少(43)异常处理基础

    Java异常是一个描述在代码段中发生的异常(也就是出错)情况的对象.当异常情况发生,一个代表该异常的对象被创建并且在导致该错误的方法中被抛出(throw).该方法可以选择自己处理异常或传递该异常.两种 ...

  5. c++ linux socket编程 c++网络编程

    声明:大部分代码来自这篇博客http://www.cnblogs.com/diligenceday/p/6241021.html, 感谢博主 思路: 思路很重要呦~~~ socket详细信息,思路:h ...

  6. [Installing Metasploit Framework on CentOS_RHEL 6]在CentOS_RHEL 6上安装Metasploit的框架【翻译】

    [Installing Metasploit Framework on CentOS_RHEL 6]在CentOS_RHEL 6上安装Metasploit的框架[翻译] 标记声明:蓝色汉子为翻译上段英 ...

  7. maven命令package、install、deploy比较

    maven生命周期(lifecycle)由各个阶段组成,每个阶段由maven的插件plugin来执行完成.生命周期(lifecycle)主要包括clean.resources.complie.inst ...

  8. Xcode6:模拟器消失了?

    今天打开Xcode,选择模拟器时发现只剩下了“iPhone 5”和“iPhone 5s”,原来什么“iPad Air”,“iPhone 4s”的都哪里去了?丢了? 别着急,依次打开“Xcode-> ...

  9. 8 -- 深入使用Spring -- 6...1 Spring支持的事务策略

    8.6.1 Spring支持的事务策略 JTA.JDBC.Hibernate Java EE应用的传统事务有两种策略:全局事务和局部事务.全局事务由应用服务器管理,需要底层服务器的JTA(Java T ...

  10. File /hbase/.tmp/hbase.version could only be replicated to 0 nodes instead of minReplication (=1).

    File /hbase/.tmp/hbase.version could only be replicated to 0 nodes instead of minReplication (=1). 这 ...