题目描述:

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.

要完成的函数:

string toGoatLatin(string S)

说明:

1、这道题给定一个字符串S,里面包含单词,大小写敏感,单词之间以空格隔开,要求把英文转化为“goat latin”,规则如下:

如果单词以元音字母a/e/i/o/u以及它们的大写形式开头,那么在单词的最后面加“ma”。

如果单词不以元音字母开头,那么把单词的首字母放到最后面,再在单词的最后面加“ma”。

第一个单词在最后再加“a”,第二个单词在最后再加“aa”,第三个单词在最后再加“aaa”,依此类推。

2、题意清晰,这道题目很容易。

直接分享给大家代码(附详解),如下:

    string toGoatLatin(string S)
{
int i=0,s1=S.size(),j=1,count=0;//i表示单词首字母位置,j表示空格位置
set<char>set1{'a','e','i','o','u','A','E','I','O','U'};
string word;//代表取出的每个单词
string res="";
while(i<s1)
{
if(S[j]==' '||S[j]=='\0')//如果碰到空格或者结束符号
{
count++;
word=S.substr(i,j-i);//取出单词,子字符串
if(set1.count(word[0])==0)//首字母非元音
{
word=word.substr(1,word.size()-1)+word[0]+"ma";
for(int k=0;k<count;k++)
word=word+'a';
}
else//首字母为元音字母
{
word=word+"ma";
for(int k=0;k<count;k++)
word=word+'a';
}
res=res+word+' ';//每个单词存储在字符串中,添加一个空格位
i=j+1;//更新i的位置
j=i+1;//更新j的位置
}
else
j++;
}
res=res.substr(0,res.size()-1);//去掉最后一次添加的空格位
return res;
}

上述代码逻辑清晰,实测6ms,因为服务器接受到的cpp submissions有限,所以没有打败的百分比。

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

    Description A sentence S is given, composed of words separated by spaces. Each word consists of lowe ...

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

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

  4. 824. Goat Latin - LeetCode

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

  5. 【Leetcode_easy】824. Goat Latin

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

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

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

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

    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. [LeetCode] 824. Goat Latin_Easy

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

  10. 824. Goat Latin

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

随机推荐

  1. iOS 10 适配 ATS(app支持https通过App Store审核)

    iOS 10 适配 ATS 一. HTTPS 其实HTTPS从最终的数据解析的角度,与HTTP没有任何的区别,HTTPS就是将HTTP协议数据包放到SSL/TSL层加密后,在TCP/IP层组成IP数据 ...

  2. etherboot无盘启动

    2001.10.30 吴峰光 本站提供对无盘启动的支持.本文就此作一简单介绍. 一.概述 无盘启动,更确切的说是网络启动,可算是最为轻松和简便的启动方式了. 目前还很少有人了解它,因为目前的软硬件条件 ...

  3. code3731 寻找道路

    将图反向,从终点spfa,把和终点没有联系的点找出来, 把它们和它们所连接的所有点删去(反向图) 然后再一遍spfa,输出最短路即可 代码: #include<iostream> #inc ...

  4. Python爬虫实战六之抓取爱问知识人问题并保存至数据库

    大家好,本次为大家带来的是抓取爱问知识人的问题并将问题和答案保存到数据库的方法,涉及的内容包括: Urllib的用法及异常处理 Beautiful Soup的简单应用 MySQLdb的基础用法 正则表 ...

  5. [Training Video - 3] [Groovy in Detail] Non-static and Static functions, initializing log inside class

    log.info "starting" // we use class to create objects of a class Planet p1 = new Planet() ...

  6. [SoapUI] context.expand 和 groovyUtils.getXmlHolder 有什么不一样

    context.expand 和 groovyUtils.getXmlHolder 有什么不一样?互相之间怎么转换 import com.eviware.soapui.support.GroovyUt ...

  7. 一个新手后端需要了解的前端核心知识点之position(一)

    以下内容是基于观看慕课网视频教程总结的知识点,边打代码边总结,符合自己的思维习惯.不是针对新手入门 我做程序的初衷是想做一个网站出来.HTML语言当然重要啊,缺什么就百度什么,很浪费时间,还是好好的打 ...

  8. Web测试实践-任务进度-Day01

    任务安排 说明:小组全体成员都参与了会议,对该实践进行分析以及对实践任务的拆分以及进行了任务的分配. 小组成员 华同学.郭同学.覃同学.刘同学.穆同学.沈同学 阶段划分 阶段1:评测被测系统 1.对被 ...

  9. 【转】JAVA 并发编程-多个线程之间共享数据

    原文地址:http://blog.csdn.net/hejingyuan6/article/details/47053409# 多线程共享数据的方式: 1,如果每个线程执行的代码相同,可以使用同一个R ...

  10. 大前端涉猎之前后端交互总结2:使用PHP进行表单数据上传与更新

    1:使用PHP进行表单上传 1.1 form表单的数据收集 HTML页面: 代码解释:核心模块是form的属性: --提交方式 :  method="post" --指定 name ...