-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolutiongoatlatin.java
51 lines (38 loc) · 998 Bytes
/
Solutiongoatlatin.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package LeetCode;
import java.util.HashSet;
import java.util.Set;
public class Solutiongoatlatin {
public static void main(String[] args) {
Solutiongoatlatin sg = new Solutiongoatlatin();
System.out.println(sg.goatLatin("I Speak Goat over add"));
}
public String goatLatin(String S) {
Set<Character> hs = new HashSet<Character>();
char[] ch = new char[] { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
for (int i = 0; i < ch.length; i++) {
hs.add(ch[i]);
}
System.out.println(hs);
StringBuilder sb = new StringBuilder();
int count = 1;
String[] words = S.split(" ");
for (String w : words) {
char first = w.charAt(0);
if (hs.contains(first)) {
sb.append(w);
} else {
sb.append(w.substring(1));
sb.append(first);
}
sb.append("ma");
int i = 0;
while (i < count) {
sb.append("a");
i++;
}
sb.append(" ");
count++;
}
return sb.toString().trim();
}
}