[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:
S
contains 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 ...
随机推荐
- E-triples II_2019牛客暑期多校训练营(第四场)
求用n个3的倍数的数按位或出数字a的方案数有多少种(0也算3的倍数) 题解 若数b的每个二进制位上的1,在a中也为1,则称b为a的子集 容易知道任意个a的子集按位或出来的结果还是a的子集 若问题改为按 ...
- POJ-3261-Milk Patterns-二分+哈希
Milk Patterns 题意: 在一串数字中,求至少连续k次的最大子序列长度: 思路: 二分加哈希: #include <cstdio> #include <iostream&g ...
- Vulkan(1)用apispec生成Vulkan库
Vulkan(1)用apispec生成Vulkan库 我的Vulkan.net库已在(https://github.com/bitzhuwei/Vulkan.net)开源,欢迎交流. apispec. ...
- java字符串加密解密
java字符串加密解密 字符串加密解密的方式很多,每一种加密有着相对的解密方法.下面要说的是java中模拟php的pack和unpack的字符串加密解密方法. java模拟php中pack: /** ...
- 实验吧CTF练习题---WEB---貌似有点难解析
实验吧web之貌似有点难 地址:http://www.shiyanbar.com/ctf/32 flag值:SimCTF{daima_shengji} 解题步骤: 1.打开题目页面,观察题目要 ...
- Hadoop&Hbase 双机热备--Pacemaker&DRBD部署
相关文章 DRBD的介绍请参考http://blog.csdn.net/rzhzhz/article/details/7103772 DRBD的部署请参考http://blog.csdn.ne ...
- sparkSql使用hive数据源
1.pom文件 <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-lib ...
- Java连载32-对象、类及其关系与定义
一.采用面向对象的方式开发一个软件,生命周期之中: (1)面向对象的分析:OOA (2)面向对象的设计:OOD (3)面向对象的编程:OOP 二.类 定义:类在现实世界世界之中是不存在的,是一个模板, ...
- Day 2 Bash shell 认识
1.拍摄虚拟机的快照 2. 什么是Bash shell? 命令解释器,将用户输入的命令,翻译给内核程序,将用户输入的指令翻译给内核 程序,内核处理完成之后将结果返回给bash. 如何打开一个bash窗 ...
- React + TypeScript 默认 Props 的处理
React 中的默认 Props 通过组件的 defaultProps 属性可为其 Props 指定默认值. 以下示例来自 React 官方文档 - Default Prop Values: clas ...