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.

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:

  • S contains only uppercase, lowercase and spaces. Exactly one space between each word.
  • 1 <= S.length <= 150.

这道题让我们将一句话转为山羊拉丁文,有几个规则,如果单词是元音开头的,那么直接在但此后加ma,(为啥不是咩mie,西方的羊就是叫ma么,不知为何想起了老友记中Janice的笑声-.-|||,但其实Janice在剧中的声音是有意为之的,看过演员本人的访谈节目,相当正常,不得不说演员啊,声优啊,都是怪物,扯远了~),如果是非元音开头的单词,那么把首字母移到末尾,并且加ma。还有就是根据当前是第几个单词,后面加几个a,估计是为了模仿羊叫声,拉长音,感觉更像绵羊音一样。此题难度不是很大,就照题目要求来做,不需要啥特别的技巧。首先为了快速检测开头字母是否为元音,我们将所有元音加入一个HashSet,注意大小写的元音都要加进去。然后要一个单词一个单词的处理,这里我们使用C++的字符串流类来快速的提取单词,对于每个提取出的单词,我们先加上一个空格,然后判断开头字母是否为元音,是的话直接加上,不然就去子串去掉首字母,然后将首字母加到末尾。后面再加上ma,还有相对应数目个a。这里我们定义一个变量cnt,初始化为1,每处理一个单词,cnt自增1,这样我们就知道需要加的a的个数了,最后别忘了把结果res的首空格去掉,参见代码如下:

class Solution {
public:
string toGoatLatin(string S) {
unordered_set<char> vowel{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
istringstream ss(S);
string res, t;
int cnt = ;
while (ss >> t) {
res += ' ' + (vowel.count(t[]) ? t : t.substr() + t[]) + "ma" + string(cnt, 'a');
++cnt;
}
return res.substr();
}
};

参考资料:

https://leetcode.com/problems/goat-latin/

https://leetcode.com/problems/goat-latin/discuss/126973/Short-C%2B%2B-solution-using-io-stringstream

https://leetcode.com/problems/goat-latin/discuss/127024/C%2B%2BJavaPython-Straight-Forward-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Goat Latin 山羊拉丁文的更多相关文章

  1. Leetcode824.Goat Latin山羊拉丁文

    给定一个由空格分割单词的句子 S.每个单词只包含大写或小写字母. 我们要将句子转换为 "Goat Latin"(一种类似于 猪拉丁文 - Pig Latin 的虚构语言). 山羊拉 ...

  2. 824. Goat Latin山羊拉丁文

    [抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...

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

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

  4. [Swift]LeetCode824. 山羊拉丁文 | Goat Latin

    A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...

  5. C#LeetCode刷题之#824-山羊拉丁文​​​​​​​(Goat Latin)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3971 访问. 给定一个由空格分割单词的句子 S.每个单词只包含大 ...

  6. LeetCode算法题-Goat Latin Easy(Java实现)

    这是悦乐书的第322次更新,第344篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第192题(顺位题号是824).给出句子S,由空格分隔的单词组成.每个单词仅由小写和大写 ...

  7. Java实现 LeetCode 824 山羊拉丁文(暴力)

    824. 山羊拉丁文 给定一个由空格分割单词的句子 S.每个单词只包含大写或小写字母. 我们要将句子转换为 "Goat Latin"(一种类似于 猪拉丁文 - Pig Latin ...

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

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

  9. LeetCode 824 Goat Latin 解题报告

    题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...

随机推荐

  1. ACM-ICPC 2018 沈阳赛区网络预赛 I Lattice's basics in digital electronics(模拟)

    https://nanti.jisuanke.com/t/31450 题意 给出一个映射(左为ascll值),然后给出一个16进制的数,要求先将16进制转化为2进制然后每9位判断,若前8位有奇数个1且 ...

  2. 查看linux的cpu信息

    # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cpuinfo| ...

  3. 【ShaderToy】新玩家~❤

    最近对shader产生了浓厚兴趣,发现一个超有意思的网站shadertoy.com,各种有意思的shader,很多都是百行以内代码实现,除了学习,作为opgl的练习场所也很不错. 分享今天看的一篇sh ...

  4. 基于STM32F1的时钟芯片DS1302驱动

    目录 DS1302.h源代码 DS1302.c源代码 DS1302.h源代码 /** ********************************************************* ...

  5. ceph简单用户管理

    列出所有用户 ceph auth list 获取指定用户 ceph auth get client.admin 新增用户并输出密钥环 ceph auth get-or-create client.ge ...

  6. Django之验证码

    一.自己生成验证码 二.极验科技互动验证码 使用前步骤:下载官网文件——pip install geetest——引入其封装的js模块 代码分为三段:生成验证码——显示验证码——验证验证码. from ...

  7. 【算法】【python实现】二叉树深度、广度优先遍历

    二叉树的遍历,分为深度优先遍历,以及广度优先遍历. 在深度优先遍历中,具体分为如下三种: 先序遍历:先访问根节点,再遍历左子树,再遍历右子树: 中序遍历:先遍历左子树,再访问根节点,再遍历右子树: 后 ...

  8. JavaScript入门学习笔记(异常处理)

    try:语句测试代码块的错误,当try中的代码块出错时执行catch中的代码块. catch:语句处理错误: throw:语句创建或抛出自定义异常. 三者一起使用可以控制程序流并生成自定义异常信息. ...

  9. Vertx.vertx()初始框图和模块

    Vertx.vertx()实例 一.构造方法 1. VertxImpl构造方法 选择 transports protocol , default select 模型 if (options.getPr ...

  10. Three teachers who make differences in my life

    人生路漫漫,从小到大,经历过九年义务教育的我们也受到过很多老师的熏陶,而已经进入大学的我也会回想起那些给我带来别样意义的老师们. 亲爱的老李-老李是我初中的班主任,也是我的历史老师.依稀记得上他的课会 ...