[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 ...
随机推荐
- P2762 太空飞行计划问题 最大权闭合子图
link:https://www.luogu.org/problemnew/show/P2762 题意 承担实验赚钱,但是要花去对应仪器的费用,仪器可能共用.求最大的收益和对应的选择方案. 思路 这道 ...
- 牛客暑假多校 H Prefix sum
题意: 现在有一个2维矩阵, 初始化为0. 并且这个矩阵是及时更新的. dp[i][j] = dp[i-1][j] + dp[i][j-1]; 现在有2种操作: 0 x y dp[1][x] += ...
- andriod开发--使用Http的Get和Post方式与网络交互通信
package com.example.a350773523.myapplication; import android.os.AsyncTask; import android.support.v7 ...
- 牛客第七场 Sudoku Subrectangles
链接:https://www.nowcoder.com/acm/contest/145/J来源:牛客网 You have a n * m grid of characters, where each ...
- JAVA - 一个for循环实现99乘法表
public class Test03 {public static void main(String[] args) { int lie = 1; for (int hang = 1; hang&l ...
- vim命令的三种模式
对于vim这个命令来讲是有三种模式的,分别是:正常模式,编辑模式以及命令模式.接下来就写一个demo作为演示 前期准备,先在本地准备一个文档,我这里就写了一个大家耳熟能详的例子,如下: 然后用rz命令 ...
- 通过脚本实现将服务器的Log实时传送到Telegram群组
首先说下需求,IT老大提出的一个需求,实现将php-laravel的应用日志实时传送到telegram的监控群组中,不用登陆服务器就可以实时查看应用的日志. 具体思路是: 先要将日志切割,并实时更新这 ...
- vim 高级功能
本文章原创首发于公众号:编程三分钟 ,文末二维码. 文本编辑.跳转.删除.复制.替换这些操作用vim确实是快:但是好像仅仅是这样根本不能说服我vim超过鼠标的地方. 花点时间弄熟这些,除了炫技意外,主 ...
- spring cloud config使用mysql存储配置文件
spring cloud config使用mysql存储配置文件 1.结构图 2.pom.xml: <?xml version="1.0" encoding="UT ...
- cmd命令查看已连接的WiFi密码
实验环境:Windows 10.命令提示符(管理员权限) 一.CMD命令查看WiFi密码 使用方法: ①.运行CMD(命令提示符) (确保无线网卡启用状态)②.输入命令查看WiFi配置文件: # ...