给定一个由空格分割单词的句子 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. 深入浅出 Java Concurrency (17): 并发容器 part 2 ConcurrentMap (2)[转]

    本来想比较全面和深入的谈谈ConcurrentHashMap的,发现网上有很多对HashMap和ConcurrentHashMap分析的文章,因此本小节尽可能的分析其中的细节,少一点理论的东西,多谈谈 ...

  2. Codec入门

    Codec 提供了一些公共的编解码实现,比如Base64, Hex, MD5等等. 工具类 package com.cxl.beanutil.util; import org.apache.commo ...

  3. 新手必踩坑之display: inline-block

    今日励志语 往日不可追,来日犹可期,祝大家2019年继往开来 迷之间隙 我们创建一个导航列表,并将其列表 item 设置为 inline-block,主要代码如下: <div class=&qu ...

  4. 11.5 临近csp·道别

    差不多到写这个东西的时候了? 嗯,按今天的日期来算的话,还有不到十天就是csp.感觉对我这种家伙来说应该算是终结了? 放在之前的话肯定会写很多东西的,不过现在大约有点不知道写什么比较合适. 所以只是祝 ...

  5. C# 制作ActiveX控件并添加到网页

    1.创建ActiveX控件——按钮 2.定义一个接口,并在控件中实现 3.部署安装 4.CAB打包 5.添加到网页中进行测试 一. 创建ActiveX控件——按钮 1.新建一个Window窗体控件库项 ...

  6. 【转载】Linux Examination

    原博地址:https://blog.csdn.net/weixin_42568655/article/details/94603660 (来自我的同学QiaoGuangtong大佬) Fundamen ...

  7. 谷歌浏览器flash被禁用解决方法

    谷歌浏览器访问设置:chrome://settings/content/flash 把要启动flash插件的网址添加进去

  8. PHP苹果推送实现(APNS)

    以下资料网上收集整理得来 1.在ios dev center制作相关证书和文件用客户端实现(不再赘述,网上很多,) 网上教程: http://blog.csdn.net/lizhenning87/ar ...

  9. 在Ubuntu Server 14.04上源码安装Odoo 9.0

    1. 更新Ubuntu服务器软件源 sudo apt-get update #更新软件源 sudo apt-get dist-upgrade #更新软件包,自动查找依赖关系 sudo shutdown ...

  10. 唱吧基于 MaxCompute 的大数据之路

    使用 MaxCompute之前,唱吧使用自建体系来存储处理各端收集来的日志数据,包括请求访问记录.埋点数据.服务器业务数据等.初期这套基于开源组件的体系有力支撑了数据统计.业务报表.风控等业务需求.但 ...