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. Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分

    cf1020D 题意: 交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同.数列保证相邻的两个差值为1或-1: 思路: 构造函数f(x) = a[x] - a[x ...

  2. == != === equals() 区别

    java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型. byte,short,char,int,long,float,double,boolean,他们之间的比较,应用双等号(==) ...

  3. SecureCRT安装及破解

    ### SecureCRT简介  > SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件. > &g ...

  4. HTML5 01. 布局、语义化标签、智能化表单、表单元素/标签/属性/事件、多媒体、类操作、自定义属性

    1.知识点 lang = “en”   所用语言是英文 文档结构更简洁 IE8一下不支持h5c3 书写更宽松 div没有语义 标签语义化:在合适的地方使用合适的标签 对seo优化友谊 网页经典布局 页 ...

  5. jQuery - 03. each、prevaAll、nextAll、获取属性、修改属性attr/val/text()、jq.height/width、offset()./position()./scrol Left/Top 、事件绑定bind、delegate、on、事件解绑、事件对象、多库共存

    each 方法 $ ( selector).each(function( index,element) {  } );   参数一表示当前元素在所有匹配元素中的索引号 参数二表示当前元素(DOM对象) ...

  6. SpringBoot初体验之整合SpringMVC

    作为开发人员,大家都知道,SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程.另外Spr ...

  7. kafka对消费者分配分区规则(Java源码)

    在上一篇 kafka topic消息分配partition规则(Java源码) 我们对生产者产生的消息分配partition规则进行了分析,那么本章我们来看看消费者是怎么样分配partition的. ...

  8. Net基础篇_学习笔记_第十一天_面向对象(面向过程与面向对象的区别/类的概念)

    1.面向过程-----> 面向对象 面向过程:面向的是完成这件事儿的过程,强调的是完成这件事儿的动作. 把大象塞进冰箱里1.打开冰箱门2.把大象塞进去,亲下大象的屁股3.关闭冰箱门 孙全 瘦小 ...

  9. unittest 常用断言

    1 assertEqual(a,b)  判断a==b 2 assertNotEqual(a,b) 判断a !=b 3 assertTrue(x)  bool (x) is True 4 assertF ...

  10. Linux 笔记 - 第十八章 Linux 集群之(一)Keepalived 高可用集群

    一.前言 Linux 集群从功能上可以分为两大类:高可用集群和负载均衡集群.此处只讲高可用集群,负载均衡放在下一篇博客讲解. 高可用集群(High Availability Cluster,简称 HA ...