[抄题]:

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,其实是每一个都加好 再拼到一起的

[一句话思路]:

给元音建立新集合

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

[画图]:

[一刷]:

  1. 拆开的字母串、字符串可以直接放在等号右边,更省事
  2. 字符串向后添加,直接用+=就行了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

以为最后再一起添加a,其实是每一个都加好 再拼到一起的

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

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

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

[关键模板化代码]:

charat(0)第0位 && .substring(1)去除第0位之后 互相对应的

[其他解法]:

[Follow Up]:

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

[代码风格] :

class Solution {
public String toGoatLatin(String S) {
//ini:store in set, res
Set<Character> vowels = new HashSet<>();
for (char c : "aeiouAEIOU".toCharArray()) {
vowels.add(c);
}
String res = "";
int i = 0, j = 0; //for loop
for (String word : S.split("\\s")) {
res += ' ' + (vowels.contains(word.charAt(0)) ? word : word.substring(1) + word.charAt(0)) + "ma"; //add a
for (j = 0, i++; j < i; j++) {
res += 'a';
}
} //return
return res.substring(1);
}
}

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

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

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

  2. [LeetCode] Goat Latin 山羊拉丁文

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

  3. Leetcode824.Goat Latin山羊拉丁文

    给定一个由空格分割单词的句子 S.每个单词只包含大写或小写字母. 我们要将句子转换为 "Goat Latin"(一种类似于 猪拉丁文 - Pig Latin 的虚构语言). 山羊拉 ...

  4. 【Leetcode_easy】824. Goat Latin

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

  5. 824. Goat Latin - LeetCode

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

  6. LeetCode 824 Goat Latin 解题报告

    题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...

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

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

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

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

随机推荐

  1. XE7/10诡异报错brcc32错误

    重新编译工程时,报错:     之前没遇到过,解决方法: 重新设置下Application Icon,再build,问题解决.

  2. Java连接Mysql的基本用法

    Java连接数据库(以MySQL为例)2007-04-05 02:23           这篇文章主要以MySQL为例讲下Java如何连接到数据库的. 当然,首先要安装有JDK(一般是JDK1.5. ...

  3. HDU - 6129 :Just do it (杨辉三角)

    There is a nonnegative integer sequence a 1...n  a1...n of length n n . HazelFan wants to do a type ...

  4. LG3391 【模板】文艺平衡树(Splay)

    题意 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 \(n,m ...

  5. 解决genymotion-arm-translation.zip无法拖拽安装的问题[转]

    1.问题由来 适用情况一:当我们启动了Genymotion模拟器后,在AndroidStudio运行app时,弹出如下错误: INSTALL_FAILED_CPU_ABI_INCOMPATIABLE ...

  6. 无法建立目录wp-content/uploads/xxxx/xx。有没有上级目录的写权限?解决办法

    首先小七已经搭建了n个wordpress网站之前没遇到过这坑爹的问题,有一天很奇怪无论是本地搭建的wp还是线上搭建的wp网站都出现了同样的问题 本地: 报错原因就是文件权限问题,所以首先就是更改wp- ...

  7. bzoj 1044 [HAOI2008]木棍分割——前缀和优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1044 前缀和优化. 但开成long long会T.(仔细一看不用开long long) #i ...

  8. thinkphp5 设置.htaccess报input file specified的解决方法

    先去检查服务器设置,这个网上方法很多就不说了,如果服务器没问题还是报这个错误的话可能和php版本有关 php5.4和以下版本的.htaccess <IfModule mod_rewrite.c& ...

  9. android签名生成和发布

    首先,我们需要一个keystore,当然已经有了的话就不用这一步了:cmd下:进入到jdk的bin目录,这样的话,android.keystore文件就会生成在这个目录下,签名的时候我们需要这个文件C ...

  10. PHP传入数组到js

    1.方式一,处理成字符串 js再将字符串处理成数组. var spec1_default = "{$spec1_default}"; spec1_default = spec1_d ...