[LeetCode] 824. Goat Latin
Description
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:
Scontains only uppercase, lowercase and spaces. Exactly one space between each word.1 <= S.length <= 150.
Analyse
如果单词以元音(a,e,i,o,u)开头,在这个单词末尾增加"ma"
"apple" -> "applema"
如果单词以辅音开头,将单词第一个字母移动到末尾,然后在单词结尾增加"ma"
"goat" -> "oatg" -> "oatgma"
对每个单词,增加单词的序号个"a"到单词的末尾,序号从1开始
"a b c" -> "ama bma cma" -> "amaa bmaaa cmaaaa"
简单题,直接上代码
string toGoatLatin(string S)
{
stringstream ss(S);
string tmp;
string result;
int index = 1;
while (ss >> tmp)
{
if (tmp[0] == 'a' || tmp[0] == 'A' ||
tmp[0] == 'e' || tmp[0] == 'E' ||
tmp[0] == 'i' || tmp[0] == 'I' ||
tmp[0] == 'o' || tmp[0] == 'O' ||
tmp[0] == 'u' || tmp[0] == 'U')
{
tmp.append("ma");
}
else
{
string first = tmp.substr(0, 1);
tmp.erase(0, 1);
tmp.append(first + "ma");
}
if (index != 1) result += " ";
result += (tmp + string(index, 'a'));
index++;
}
return result;
}
[LeetCode] 824. Goat Latin的更多相关文章
- LeetCode 824 Goat Latin 解题报告
题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...
- LeetCode 824. Goat Latin (山羊拉丁文)
题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...
- 824. Goat Latin - LeetCode
Questioin 824. Goat Latin Solution 题目大意:根据要求翻译句子 思路:转换成单词数组,遍历数组,根据要求转换单词 Java实现: 用Java8的流实现,效率太低 pu ...
- 【Leetcode_easy】824. Goat Latin
problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...
- 【LeetCode】824. Goat Latin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode&Python] Problem 824. Goat Latin
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- [LeetCode] 824. Goat Latin_Easy
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- 824. Goat Latin山羊拉丁文
[抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...
- 824. Goat Latin
class Solution { public: string toGoatLatin(string S) { S.push_back(' '); //add a space that the loo ...
随机推荐
- Badboy中创建Suite, test, step和Template
参考: http://leafwf.blog.51cto.com/872759/1111744 http://www.51testing.com/html/00/130600-1367743.html ...
- POJ-1213 How Many Tables( 并查集 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...
- 2015 JSOI冬令营训练 彩色格子 题解
解析 棋盘上黑白格染色.曼哈顿距离偶数:奇偶性相同. 枚举有几种颜色分到白格,组合数计算即可. 注意预处理,时间还是比较宽裕的. 为了不重复计数,考虑枚举严格用了i种颜色,我们再枚举分配j种给白集合. ...
- python实现去掉空行
# coding = utf-8def clearBlankLine(): file1 = open('text1.txt', 'r', encoding='utf-8') # 要去掉空行的文件 fi ...
- DB-Lib error message 20002, severity 9
完整报错内容:20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (I0.185.4 ...
- Python编译器及Sublime Text3安装及开发环境配置
1.初学Python,你需要一个好的开发编辑器 在选择Python编辑器时,可能纠结于那个Python的版本更好一些,在Python2.x和Python3.x版本中, Python3.x版本更好一些, ...
- 小程序组件 scroll-view 横向滚动条无效
小程序组件 scroll-view 中分别有上下竖向滑动和左右横向滑动,在这次项目中刚好需要用到横向滑动,但在测试过程中发现横向滑动没有了效果(静止在那里没移动过,并且换行了),经调试发现: 1.sc ...
- CCF 模拟试题——出现次数最多的数 官方答案解析及自己写的正确答案
前几天知道的CCF计算机职业资格认证考试,觉得好像比软考含金量高一些,就去了解了一下,做了模拟试题中的 “出现次数最多的数” 这道题,我的算法和官方答案算法不同,个人觉得觉得官方的好一点,没那么繁琐, ...
- WTM送书活动:向更遥远的星辰大海起航~
是的,没错~ 这一篇不是大老刘写的.哈哈~ 啥? 你想知道为啥? 大老刘为了你们不加班,熬夜改BUG,姑娘不乐意了... 然后... 后面请自行脑补~ 哎~生活还要继续鸭.... 那么,接下来由我陪 ...
- Hadoop学习笔记之HBase Shell语法练习
Hadoop学习笔记之HBase Shell语法练习 作者:hugengyong 下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令 ...