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. hdu-6579 Operation

    题目链接 Operation Problem Description There is an integer sequence a of length n and there are two kind ...

  2. CodeForces 283C World Eater Brothers

    World Eater Brothers 题解: 树DP, 枚举每2个点作为国家. 然后计算出最小的答案. 首先我们枚举根, 枚举根了之后, 我们算出每个点的子树内部和谐之后的值是多少. 这样val[ ...

  3. 【转载】Why Learning to Code is So Damn Hard By Erik Trautman

    原文网址:https://www.thinkful.com/blog/why-learning-to-code-is-so-damn-hard/ 在罗老师的<算法竞赛 入门到进阶>总看到了 ...

  4. poj 1661 Help Jimmy(记忆化搜索)

    题目链接:http://poj.org/problem?id=1661 一道还可以的记忆化搜索题,主要是要想到如何设dp,记忆化搜索是避免递归过程中的重复求值,所以要得到dp必须知道如何递归 由于这是 ...

  5. codeforces E. Phone Talks(dp)

    题目链接:http://codeforces.com/contest/158/problem/E 题意:给出一些电话,有打进来的时间和持续的时间,如果人在打电话,那么新打进来的电话入队,如果人没有打电 ...

  6. 宝塔Linux面板命令

    安装宝塔 Centos安装脚本 yum install -y wget && wget -O install.sh http://download.bt.cn/install/inst ...

  7. Docker详解(二)

    目录 1.Docker常用命令 1.1 镜像命令 1.2 容器命令 1.2.1 常用的容器命令 1.2.2 重要的容器命令 序言:上一章我们初步介绍了一下Docker的概念,那么这次我们着手于Dock ...

  8. CNCF 宣布成立应用交付领域小组,正式开启云原生应用时代

    作者|赵钰莹 作为云原生领域的顶级开源社区, Cloud Native Computing Foundation (云原生基金会,以下简称 CNCF)近日宣布成立 Application Delive ...

  9. UGUI_游戏界面开发Demo001

    1.Alt+Stretch:快速拉伸匹配至画布,与父类大小保持一致. 2.Anchors锚点:实现屏幕自适应 图片也可以实现自适应.Target Graphic (目标图),点击的时候,控件的效果用在 ...

  10. win7右下角声音图标不见了

    场景:开机后发生右下角的声音图标不见了,马上google,可能性有两种图标隐藏或者系统错误 隐藏处理方式:右下角下打开自定义--> 将它调为显示和通知(发生不好使,估计是系统错误) 系统错误处理 ...