题目要求

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. C++调用外部应用程序

    很多时候,我们的程序需要调用DOS命令,通过Dos命令调用其他程序从而完成所需要完成的功能.比如,调用Dos程序PKZIP完成ZIP包的解压缩,调用SVN完成文件的更新或者上传.但是在程序运行时又要求 ...

  2. C++:重载全局new/delete实现跨平台多线程内存检测

    Reference: https://blog.csdn.net/u014023615/article/details/39551191 Reference: https://blog.csdn.ne ...

  3. [Linux]linux如何把文件切成多块

    转:https://blog.csdn.net/qq_42224274/article/details/80880914 将一个大文件分成若干个小文件方法例如将一个BLM.txt文件分成前缀为 BLM ...

  4. WebMisSharp升级说明,最新版本1.6.0

    尊敬的C3 AM.C3 FX.WebMisSharp用户您好: 非常感谢长期来您对WebMisSharp系列产品的支持,您的使用和反馈是我们进步的最大动力.在你们的帮助下我们又向前迈进了一步,我们功能 ...

  5. MSM8953 audio dts 代码跟踪

    跟一下msm8953音频的dts. msm8953-audio-mtp.dtsi &int_codec { status = "okay"; qcom,model = &q ...

  6. c# 正则匹配对称括号

    https://stackoverflow.com/questions/7898310/using-regex-to-balance-match-parenthesis

  7. Java中浮点数的处理

    import java.text.DecimalFormat; String addGold = String.valueOf(new DecimalFormat("0").for ...

  8. [hadoop] hadoop 运行 wordcount

    讲准备好的文本文件放到hdfs中 执行 hadoop 安装包中的例子 [root@hadoop01 mapreduce]# hadoop jar hadoop-mapreduce-examples-2 ...

  9. [Object Tracking] Deep Boundary detection Tech

    AR的要点之一便是精确跟踪 From: https://zhuanlan.zhihu.com/p/26848831?refer=dlclass Boundary Detection Benchmark ...

  10. Web文件上传方法总结大全

    1. 表单上传 这是传统的form表单上传,使用form表单的input[type=”file”]控件,可以打开系统的文件选择对话框,从而达到选择文件并上传的目的,它的好处是多浏览器兼容,它是web开 ...