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 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"

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为最后再一起添加a,其实是各自自己加自己的。

[一句话思路]:

给元音建立新集合

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

for (j = 0, i++; j < i; ) 两个指针中j 每次清0,i不清零 来控制逐渐递增。头次见

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

原文地址:https://www.cnblogs.com/immiao0319/p/8975027.html

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

824. Goat Latin山羊拉丁文的相关文章

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

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

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',

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刷题总结

对于不是对于单个字符进行操作,而是对单词进行操作时,可以选择一些库函数 常用的有: 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简单题61~80

61.思路:bin# 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量).# 示例 1:# 输入:00000000000000000000000000001011# 输出:3# 解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'.# 示例 2:# 输入:00000000000000000000000010000000# 输出:1# 解释:输入的二进制串 0000000000000000

【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