[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 ...
随机推荐
- HDU-3038How Many Answers Are Wrong权值并查集
How Many Answers Are Wrong 题意:输入一连串的区间和,问和前面的矛盾个数: 思路:我在做专题,知道是并查集,可是还是不知道怎么做,学了一下权值并查集和大佬的优秀思路,感觉回了 ...
- 矩阵快速幂 hud 1575 Tr A 模板 *
Tr A Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 弄懂goroutine调度原理
goroutine简介 golang语言作者Rob Pike说,"Goroutine是一个与其他goroutines 并发运行在同一地址空间的Go函数或方法.一个运行的程序由一个或更多个go ...
- Maven学习归纳(二)——几个常用命令解析
Maven的常用命令 第一次执行命令的时候,因为需要下载执行命令的基础环境,所以会从远程仓库下载该环境到本地仓库中 运行mvn命令,必须在pom.xml文件所在的目录 一. JavaProject的p ...
- CF785D Anton and School – 2
- 每个Java开发人员都应该知道的10个基本工具
大家好,我们已经在2019年的第9个月,我相信你们所有人已经在2019年学到了什么,以及如何实现这些目标.我一直在写一系列文章,为你提供一些关于你可以学习和改进的想法,以便在2019年成为一个更好的. ...
- java获取电脑mac物理地址
import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import ...
- Python集训营45天—Day04 (函数)
目录 1. 函数介绍 2. 函数的参数 3. 模块与函数 4. 递归函数 5. 匿名函数 6. 多返回值 python 的学习已经进入到第四天,前面几章我们已经学会了基本的变量操作,以及分支结构和循环 ...
- CabloyJS带你轻松走进NodeJS全栈开发-免费课程 作者亲授
课程说明 B站直播 为回馈新老同学对开源框架CabloyJS的支持与厚爱,快速而轻松的开启NodeJS全栈开发之旅.2019年9月5日至9月11日在B站开启了一波免费直播培训课程 课程信息,请点击链接 ...
- 10 (OC)* Coretext
CoreText实现 看上图,我们可以知道,一个View包括CTFrame,CTFrame中间包括许多行CTLine,而一个CTLine中包括许多CTRun 我们主要说说CTLine和CTRun ...