前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode-824-Goat Latin(字符串的处理)

leetcode-824-Goat Latin(字符串的处理)

作者头像
chenjx85
发布2018-05-21 18:17:17
8140
发布2018-05-21 18:17:17
举报

题目描述:

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:

代码语言:javascript
复制
Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

Example 2:

代码语言:javascript
复制
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、题意清晰,这道题目很容易。

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

代码语言:javascript
复制
    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有限,所以没有打败的百分比。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述:
  • 要完成的函数:
  • 说明:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档