题目要求

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. idea git 使用

    第一部  测试  本地git 是否已经成功安装 centos 7 发行版默认已经安装 第二部: 测试 github 连接是否成功,需要输入用户密码 第三部:创建git项目管理两种方式 1. 菜单 VC ...

  2. Android Gallery实现3D相册(附效果图+Demo源码)

    今天因为要做一个设置开机画面的功能,主要是让用户可以设置自己的开机画面,应用层需要做让用户选择开机画面图片的功能.所以需要做一个简单的图片浏览选择程序.最后选用Gallery作为基本控件.加入了一些炫 ...

  3. STM32f103的数电采集电路的DMA设计和使用优化程序

    DMA,全称为:Direct Memory Access,即直接存储器访问.DMA传输方式无需CPU直接控制传输,也没有中断处理方式那样保留现场和恢复现场的过程,通过硬件为RAM与I/O设备开辟一条直 ...

  4. (转)java 层调用Jni(Ndk) 持久化c c++ 对象

    对于Jni(Ndk) 很多人应该都有印象,Android的ndk接触到的机会相对会比较多,本例子以android平台为例,pc端的话就以简单的windows为例, 编码完用vs 或是 gcc进行编译成 ...

  5. centos6.5安装mongodb2.6

    下载地址:http://www.mongodb.org/downloads 解压命令:tar zxf mongodb-linux-i686-2.6.0.tgz 存放目录:/usr/local/mong ...

  6. R语言reads.table 自动将字符串变成了逻辑值

    今天遇到了一个问题,文件中有一列的值为全为F, 用read.table 读取的时候,自动将F 变成了false 对于这样的转换,可以通过 colClass 参数控制 colClass 参数指定每一列的 ...

  7. [JS] ECMAScript 6 - Array : compare with c#

    扩展运算符(spread) 先复习下 rest 参数. (1) argument模式,但不够好. // https://blog.csdn.net/weixin_39723544/article/de ...

  8. invalid bound statement (not found)解决办法

    最近搭建一个web项目后端SSM框架练手,使用Maven管理,遇到一个深坑invalid bound statement (not found),就是mapper文件扫描不到!!! 先上整合过程,解决 ...

  9. ssh登录慢解决办法

    这两天ssh登录局域网的一台服务器非常慢,严重影响工作效率,怎么办?查了一下网上的解决办法,总结一下: 使用命令ssh -v xxx@x.x.x.x 可以看到debug信息,找到问题出在哪: debu ...

  10. jenkins管理

    1.1 重启,重载,关闭 http://10.0.0.51:8080/jenkins/restart     重启 http://10.0.0.51:8080/jenkins/reload       ...