Problem (and this is a very stupid problem...)
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.
class Solution { public String toGoatLatin(String S) { String[] words = S.split(" "); String as = ""; StringBuilder sb = new StringBuilder(); Setvowel = new HashSet<>(); vowel.add("a"); vowel.add("e"); vowel.add("i"); vowel.add("o"); vowel.add("u"); vowel.add("A"); vowel.add("E"); vowel.add("I"); vowel.add("O"); vowel.add("U"); for (int i = 0; i < words.length; i++) { as += "a"; char first = words[i].charAt(0); if (vowel.contains(first)) { sb.append(words[i]+"ma"+as+" "); } else { sb.append(words[i].substring(1)+first+"ma"+as+" "); } } return sb.toString().trim(); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/77266.html
摘要:题目链接题目分析给定一个句子,由大小写英文字母组成,以空格为单词的分割。即,在第个单词按以上规则转换完成后,再加个。分割后,判断首字母是否不是元音。不是元音,则将第一个字母移到最后。给字符串末尾添加。 D60 824. Goat Latin 题目链接 824. Goat Latin 题目分析 给定一个句子,由大小写英文字母组成,以空格为单词的分割。 按以下规则修改单词: 如果一个单词...
摘要:把直接加到声明前面就可以省略无论怎样输出,输入的时候都需要。其实这种导出方式可以看成是命名导出的变种,只不过把命名写成了。对应输入的例子参考文章详解中与的用法和区别我在 ES6中export一般的用法有两种 命名导出(Named exports) 默认导出(Default exports) 命名导出(Named exports) 就是每一个需要输出的数据类型都要有一个name,统一...
摘要:与此同时,小组也一同致力于项目,参与了很多动物命名的项目,其中有广为人知的项目。主控服务器将所有更新操作序列化,利用协议将数据更新请求通知所有从属服务器,保证更新操作。在术语下,节点被称为。命名为的,由系统自动生成,用配额管理。 ZooKeeper 介绍 ZooKeeper(wiki,home,github) 是用于分布式应用的开源的分布式协调服务。通过暴露简单的原语,分布式应用能在之...
阅读 3501·2021-11-24 09:39
阅读 780·2019-08-30 14:22
阅读 3031·2019-08-30 13:13
阅读 2310·2019-08-29 17:06
阅读 2918·2019-08-29 16:22
阅读 1255·2019-08-29 10:58
阅读 2427·2019-08-26 13:47
阅读 1627·2019-08-26 11:39