-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL824.java
More file actions
37 lines (36 loc) · 1.42 KB
/
L824.java
File metadata and controls
37 lines (36 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class Solution824 {
class Solution {
/**
* 824: Goat Latin https://leetcode.com/problems/goat-latin/description/
* This solution leverages Java 8's stream features.
*
* @param S String
* Input string
* @return Goat latin string
* @timeComplexity O(n) where n is the number of characters in input string. There are constant number of vowels
* in line 20 to be iterated.
* @spaceComplexity O(1) the string buffer is only for output, it is not auxiliary.
*/
public String toGoatLatin(String S) {
String[] splits = S.split(" ");
return String.join(" ", IntStream.range(0, splits.length).mapToObj(i -> {
StringBuffer sb = new StringBuffer();
// Its a vowel
if ("aeiouAEIOU".chars().anyMatch(c -> c == splits[i].charAt(0))) {
sb.append(splits[i]);
// Its a consonant
} else {
sb.append(splits[i].substring(1));
sb.append(splits[i].charAt(0));
}
sb.append("maa");
for (int j = 0; j < i; j++) {
sb.append('a');
}
return sb.toString();
}).collect(Collectors.toList()));
}
}
}