824. Goat Latin

 1 class Solution
 2 {
 3 public:
 4     string toGoatLatin(string S)
 5     {
 6         S.push_back(‘ ‘);         //add a space that the loop can deal with the last word
 7         string a(150,‘a‘);        // string to add ‘a‘
 8         unordered_set<char> vowel={‘a‘,‘e‘,‘i‘,‘o‘,‘u‘,‘A‘,‘E‘,‘I‘,‘O‘,‘U‘};
 9         int front=0;
10         int len=S.size();
11         int alen=1;
12         string res;
13         for(int i=0;i<len;i++)      //once we meet a space ,we can deal with the word before it
14         {
15             if(S[i]==‘ ‘)
16             {
17                 string cur=S.substr(front,i-front);
18                 if(vowel.find(cur[0])==vowel.end())
19                 {
20                     cur.push_back(cur[0]);
21                     cur.erase(cur.begin());
22                 }
23                 res=res+cur+"ma"+a.substr(0,alen)+" ";
24                 alen++;
25                 front=i+1;
26             }
27         }
28         res.pop_back();    //delete the extra space
29         return res;
30     }
31 };

把原字符串剪断,一个单词一个单词处理,再把单词拼接成结果即可

原文地址:https://www.cnblogs.com/zhuangbijingdeboke/p/9190601.html

时间: 2024-10-09 06:19:09

824. Goat Latin的相关文章

824. Goat Latin山羊拉丁文

[抄题]: 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 La

824. Goat Latin - Easy

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 ar

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

题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一个char 加到最后: 然后添加“ma” 和 “a“ 到最后: 添加新的"a": 把新的 word 加入 result,还要记得加入空格. Java Solution: Runtime beats 62.66% 完成日期:10/12/2018 关键词:String 关键点:利用HashSe

LeetCode 题解之Goat Latin

1.问题描述 2.问题分析 将句子拆分成单词,然后放入vector中,对每一个单词做改变,最后连成句子返回. 3.代码 1 string toGoatLatin(string S) { 2 vector<string> words; 3 for(string::iterator it = S.begin() ; it != S.end(); ++it ){ 4 string::iterator it_i = it ; 5 while( it_i != S.end() && is

*LeetCode--Goat Latin

Goat Latin 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 Go

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

LeetCode--String刷题总结

对于不是对于单个字符进行操作,而是对单词进行操作时,可以选择一些库函数 常用的有: 1. str.split(" ");  按照空格将字符串进行划分,得到字符串数组 2. str.substring(i, j); 得到字符串的子串 [ i, j ) 3. str.contains("s1"); str中是否包含 s1字符串 4. str.charAt(7); str在7位置上的char型字符 5. str.trim(); 去除str两侧的空格 应用:山羊拉丁文Goa

LeetCode-824 划水记录3

给定一个由空格分割单词的句子 S.每个单词只包含大写或小写字母. 我们要将句子转换为 "Goat Latin"(一种类似于 猪拉丁文 - Pig Latin 的虚构语言). 山羊拉丁文的规则如下: 如果单词以元音开头(a, e, i, o, u),在单词后添加"ma".例如,单词"apple"变为"applema". 如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma".

【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranking: 440/2797.这次题目似乎比较简单,因为我比赛的时候前三题全做出来了(1:12:39),然后第四题有思路,正在写,没写完,比赛完了写完提交也对了. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [821]