GoatLatin [source code]
public class GoatLatin {
static
/****************************************************************************/
class Solution {
public String toGoatLatin(String S) {
StringBuilder sb = new StringBuilder ();
int num_a = 1;
for (String word : S.trim ().split ("\\s+")) {
if ("aeiouAEIOU".contains (word.charAt (0) + "")) {
sb.append (word);
sb.append ("ma");
} else {
sb.append (word.substring (1));
sb.append (word.charAt (0));
sb.append ("ma");
}
for (int i = 0; i < num_a; i++) {
sb.append ('a');
}
num_a++;
sb.append (" ");
}
return sb.toString ().trim ();
}
}
/****************************************************************************/
public static void main(String[] args) {
GoatLatin.Solution tester = new GoatLatin.Solution();
String[] inputs = {
"I speak Goat Latin", "Imaa peaksmaaa oatGmaaaa atinLmaaaaa",
};
}
}
看了一下例子, 一个需要注意的是可能可以用memo来挡一下, 如果重复出现的单词太多的话;
好像也没什么用, memo需要有返回值, 如果设计返回值的一个helper函数, 最后就免不了要来回跟string进行转换, 最后速度也就下去了; 一个StringBuilder走到底算了;
最后也没有什么特别需要注意的地方;
editorial
Approach #1: String [Accepted]
Intuition
We apply the steps given in the problem in a straightforward manner. The difficulty lies in the implementation.
Algorithm
For each word in the sentence split, if it is a vowel we consider the word, otherwise we consider the rotation of the word (either word[1:] + word[:1] in Python, otherwise word.substring(1) + word.substring(0, 1) in Java).
Afterwards, we add "ma", the desired number of "a"'s, and a space character.
class Solution {
public String toGoatLatin(String S) {
Set<Character> vowel = new HashSet();
for (char c: new char[]{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'})
vowel.add(c);
int t = 1;
StringBuilder ans = new StringBuilder();
for (String word: S.split(" ")) {
char first = word.charAt(0);
if (vowel.contains(first)) {
ans.append(word);
} else {
ans.append(word.substring(1));
ans.append(word.substring(0, 1));
}
ans.append("ma");
for (int i = 0; i < t; i++)
ans.append("a");
t++;
ans.append(" ");
}
ans.deleteCharAt(ans.length() - 1);
return ans.toString();
}
}
N^2时间, N^空间;
UNFINISHED
uwi: 4分钟:
class Solution {
public String toGoatLatin(String S) {
String[] s = S.split(" ");
StringBuilder sb = new StringBuilder();
for(int i = 0;i < s.length;i++){
if("AEIOUaeiou".indexOf(s[i].charAt(0)) >= 0){
sb.append(" " + s[i] + "ma");
}else{
sb.append(" " + s[i].substring(1) + s[i].charAt(0) + "ma");
}
for(int j = 0;j < i+1;j++){
sb.append("a");
}
}
return sb.substring(1);
}
}
Problem Description
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 <= 100.
Difficulty:Easy
Total Accepted:1.5K
Total Submissions:2.7K
Contributor:awice
Companies
facebook
Related Topics
string