给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。

我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。

山羊拉丁文的规则如下:

  • 如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。

例如,单词"apple"变为"applema"。

  • 如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。

例如,单词"goat"变为"oatgma"。

  • 根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始。

例如,在第一个单词后添加"a",在第二个单词后添加"aa",以此类推。

返回将 S 转换为山羊拉丁文后的句子。

示例 1:

输入: "I speak Goat Latin" 输出: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

示例 2:

输入: "The quick brown fox jumped over the lazy dog" 输出: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

说明:

  • S 中仅包含大小写字母和空格。单词间有且仅有一个空格。
  • 1 <= S.length <= 150。

忘记判定元音大写的情况

class Solution {
public:
string toGoatLatin(string S) {
int len = S.size();
if(len == 0)
return "";
string str = "";
string temp = "";
int cnt = 0;
for(int i = 0; i < len; i++)
{
if(i == len - 1 || S[i] != ' ')
{
temp += S[i];
}
if(S[i] == ' ' || i == len - 1)
{
cnt++;
if(temp == "")
continue;
if(temp[0] == 'a' || temp[0] == 'e' || temp[0] == 'i' || temp[0] == 'o' || temp[0] == 'u'
|| temp[0] == 'A' || temp[0] == 'E' || temp[0] == 'I' || temp[0] == 'O' || temp[0] == 'U')
{
temp += "ma";
}
else
{
char c = temp[0];
for(int j = 0; j < temp.size() - 1; j++)
{
temp[j] = temp[j + 1];
}
temp[temp.size() - 1] = c;
temp += "ma";
}
for(int j = 0; j < cnt; j++)
{
temp += 'a';
}
if(cnt != 1)
str += ' ';
str += temp;
temp = "";
}
}
return str;
}
};

Leetcode824.Goat Latin山羊拉丁文的更多相关文章

  1. [LeetCode] Goat Latin 山羊拉丁文

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

  2. 824. Goat Latin山羊拉丁文

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

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

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

  4. [Swift]LeetCode824. 山羊拉丁文 | Goat Latin

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

  5. C#LeetCode刷题之#824-山羊拉丁文​​​​​​​(Goat Latin)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3971 访问. 给定一个由空格分割单词的句子 S.每个单词只包含大 ...

  6. Java实现 LeetCode 824 山羊拉丁文(暴力)

    824. 山羊拉丁文 给定一个由空格分割单词的句子 S.每个单词只包含大写或小写字母. 我们要将句子转换为 "Goat Latin"(一种类似于 猪拉丁文 - Pig Latin ...

  7. LeetCode算法题-Goat Latin Easy(Java实现)

    这是悦乐书的第322次更新,第344篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第192题(顺位题号是824).给出句子S,由空格分隔的单词组成.每个单词仅由小写和大写 ...

  8. LeetCode 824 Goat Latin 解题报告

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

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

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

随机推荐

  1. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu[转]

    容器的主要职责有两个:存放元素和浏览元素.根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器. 用迭代器封装对动态数组的遍历:  1  2{<HeadFirst设计模式& ...

  2. 转:linux select 多路复用机制

    源地址:http://blog.csdn.net/turkeyzhou/article/details/8609360 2013-02-25 14:18 442人阅读 评论(1) 收藏 举报   目录 ...

  3. C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客

    原文:C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客 public static List<T> CreateTarInterface& ...

  4. kafka理论

    一.消息队列,简称MQ,message queue 生产者:生存数据写到kafka,持久化到硬盘.对同一个Topic来讲,生产者通常只有‘一个’(可以多并发)数据保存时常可以配置,默认保存七天. 消费 ...

  5. php数据几行代码导出到excel(非插件)

    <?php header("Content-type:application/vnd.ms-excel"); header("Content-Disposition ...

  6. SQL 约束和表修改语句

    1.约束作用: 约束的目的就是确保表中的数据的完整性 2.常用的约束类型如下 主键约束:(Primary Key constraint) 要求主键列唯一,并且不允许为空    唯一约束:(Unique ...

  7. 汉诺塔III HDU - 2064

    汉诺塔III HDU - 2064   约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘全部移到右 ...

  8. day38 01-Spring框架的概

    Action里面调Service,Service里面调DAO,在Action里面new一个Service,在Service里面new一个DAO.有了Spring之后可以不用new对象了.AOP里面有很 ...

  9. Zookeeper 扫盲

    Zookeeper 扫盲 :disappointed_relieved: 配置文件详解: tickTime:基本事件单元,以毫秒为单位,这个时间作为 Zookeeper 服务器之间或客户端之间维持心跳 ...

  10. 基于spring-boot的测试桩设计-添加配置文件(properties)

    编写测试时,有些内容可以放到配置文件中. 第一步:新增配置文件 conf.properties 第二步:编写配置文件类 MockConf package mock.mockdemo.conf; imp ...