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的更多相关文章

  1. LeetCode 824 Goat Latin 解题报告

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

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

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

  3. 824. Goat Latin - LeetCode

    Questioin 824. Goat Latin Solution 题目大意:根据要求翻译句子 思路:转换成单词数组,遍历数组,根据要求转换单词 Java实现: 用Java8的流实现,效率太低 pu ...

  4. 【Leetcode_easy】824. Goat Latin

    problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...

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

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

  6. [LeetCode&Python] Problem 824. Goat Latin

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

  7. [LeetCode] 824. Goat Latin_Easy

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

  8. 824. Goat Latin山羊拉丁文

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

  9. 824. Goat Latin

    class Solution { public: string toGoatLatin(string S) { S.push_back(' '); //add a space that the loo ...

随机推荐

  1. SDU暑期集训排位(3)

    B. Mysterious LCM 做法 保留 \(a_i|x\) 的元素,其它元素解体. \(a_i\) 的某个质因子的指数,要和 \(x\) 的这个质因子一样多,才有贡献,否则这个质因子它在划水啊 ...

  2. Codeforces 889F Letters Removing(二分 + 线段树 || 树状数组)

    Letters Removing 题意:给你一个长度为n的字符串,然后进行m次删除操作,每次删除区间[l,r]内的某个字符,删除后并且将字符串往前补位,求删除完之后的字符串. 题解:先开80个set ...

  3. ‎Cocos2d-x 学习笔记(24) ParticleSystem ParticleSystemQuad

    1. ParticleSystem ParticleData是存储粒子数据的类,ParticleSystem会关联一个ParticleData对象. ParticleSystem直接继承了Node.T ...

  4. InnoDB在MySQL默认隔离级别下解决幻读

    1.结论 在RR的隔离级别下,Innodb使用MVVC和next-key locks解决幻读,MVVC解决的是普通读(快照读)的幻读,next-key locks解决的是当前读情况下的幻读. 2.幻读 ...

  5. PathMarker:命令行快速编辑跳转工具(配合git,find等)

    简介 当使用git, find等工具时,一般需要进一步打开文件或跳转目录. 通常的做法是使用鼠标复制黏贴,或者手工输入. PathMarker提供一种快速通过标号,编辑相关文件,跳转相关目录的方法,提 ...

  6. android Drawable Resource学习

    转载自: http://blog.csdn.net/true100/article/details/52316423 http://blog.csdn.net/true100/article/deta ...

  7. gemfire基本使用以及spring-data-gemfire的使用

    1.安装程序的使用 locator 启动locator gfsh>start locator --name=locator1 指定端口启动 gfsh>start locator --nam ...

  8. 安全性测试:OWASP ZAP 2.8 使用指南(二):ZAP基础操作

    ZAP桌面应用 ZAP桌面应用由以下元素组成: 1. 菜单栏  – 提供多种自动化和手动工具的访问 2. 工具栏  – 提供快速访问最常用组件的用户接口 3. 树结构窗口  – 展示被测网站树结构和脚 ...

  9. springmvc request foward 和 redirect

    ---恢复内容开始--- 最近在实现那个学生信息录入的时候,先是在添加学生的页面添加完,然后想直接调用Conroller层遍历学生的方法,我的意思就是在contoller一个方法怎么直接调用另外一个方 ...

  10. Java8之熟透Lambda表达式

    一.Lambda简述 1.1.Lambda概述 ​ Lambda 表达式可以理解为简洁地表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还有一个可以抛出的异常列表. ...