[LeetCode] 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"
Notes:
S
contains only uppercase, lowercase and spaces. Exactly one space between each word.1 <= S.length <= 150
.
山羊拉丁文。
给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。
我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。
山羊拉丁文的规则如下:
如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。
例如,单词"apple"变为"applema"。如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
例如,单词"goat"变为"oatgma"。根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始。
例如,在第一个单词后添加"a",在第二个单词后添加"aa",以此类推。
返回将 S 转换为山羊拉丁文后的句子。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/goat-latin
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这个题不难,按照规则做就行了。如果面试遇到这种题,不能心急,耐心把条件看完再动手。
时间O(n)
空间O(n)
Java实现
1 class Solution { 2 public String toGoatLatin(String S) { 3 HashSet<Character> vowel = new HashSet<>(); 4 for (char c : "aeiouAEIOU".toCharArray()) { 5 vowel.add(c); 6 } 7 StringBuilder res = new StringBuilder(); 8 int count = 0; 9 for (String cur : S.split(" ")) { 10 count++; 11 if (count > 1) { 12 res.append(" "); 13 } 14 if (vowel.contains(cur.charAt(0))) { 15 res.append(cur); 16 } else { 17 res.append(cur.substring(1) + cur.charAt(0)); 18 } 19 res.append("ma"); 20 for (int j = 0; j < count; j++) { 21 res.append("a"); 22 } 23 } 24 return res.toString(); 25 } 26 }
标签:Latin,word,res,Goat,单词,824,LeetCode,append 来源: https://www.cnblogs.com/cnoodle/p/13532854.html