题目要求

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. 假设分配给命令的连接位于本地挂起事务中,ExecuteReader 要求命令拥有事务。命令的 Transaction 属性尚未初始化

    {System.InvalidOperationException: 假设分配给命令的连接位于本地挂起事务中.ExecuteReader 要求命令拥有事务.命令的 Transaction 属性尚未初始 ...

  2. Brainfuck反汇编(Python)

    global cs global ip global ss #global sp global ds global bp global tab global out #cs='++++++++++[& ...

  3. svn常见错误解决

    Svn冲突导致锁住的解决方案:错误码:svn: E155037: Previous operation has not finished; run 'cleanup' if it was interr ...

  4. PE病毒初探——向exe注入代码

    PE文件其实就是Windows可执行文件,关于它的一些简要介绍摘自百度: PE文件被称为可移植的执行体是Portable Execute的全称,常见的EXE.DLL.OCX.SYS.COM都是PE文件 ...

  5. svn-checkout后,循环遍历查找包含某字符串的文件

    这里涉及几个知识点: 1.安装subversion,不多说了,网上有教程 2.循环遍历所有目录层级,找相 关文件 #!/bin/bash #########svn checkout项目出来 svn_d ...

  6. java-信息安全(十四)-初探SSL

    原文地址 http://snowolf.iteye.com/blog/397693 我们需要构建一个由CA机构签发的有效证书,这里我们使用上文中生成的自签名证书zlex.cer     这里,我们将证 ...

  7. 安卓开发笔记——丰富多彩的TextView

    随手笔记,记录一些东西~ 记得之前写过一篇文章<安卓开发笔记——个性化TextView(新浪微博)>:http://www.cnblogs.com/lichenwei/p/4411607. ...

  8. duilib进阶教程 -- XML嵌套及自定义控件 (4)

    代码下载:http://download.csdn.net/detail/qq316293804/6433937 之前入门教程里已经讲过了自定义控件,这里借着迅雷播放器再次举个例子. 1.我们先给迅雷 ...

  9. supervisor的command执行两条命令

    如下supervisor的进程的comand配置参数只能写一个命令 1.要执行多条命令,可以写个sh文件包含多条命令,然后sh -x   xxxx.sh,但这样又多了一个文件, 2.把所有命令放在字符 ...

  10. SpringBoot(十六)-- 使用外部容器运行springBoot项目

    spring-boot项目需要部署在外部容器中的时候,spring-boot导出的war包无法再外部容器(tomcat)中运行或运行报错. 为了解决这个问题,需要移除springBoot自带的tomc ...