-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoatLatin.java
60 lines (54 loc) · 1.46 KB
/
GoatLatin.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
52
53
54
55
56
57
58
59
60
package String;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* Author - archit.s
* Date - 30/08/18
* Time - 1:05 PM
*/
public class GoatLatin {
public String toGoatLatin(String S) {
Character c[] = {'a','e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
Set<Character> s = new HashSet<>(Arrays.asList(c));
S = S.trim();
StringBuilder sb = new StringBuilder();
int space = 1;
int i = 0;
while(i<S.length()){
boolean vowel = s.contains(S.charAt(i));
boolean first = true;
char firstC = 'a';
while(i< S.length() && S.charAt(i) != ' '){
if(first){
if(!vowel){
firstC = S.charAt(i);
}
else{
sb.append(S.charAt(i));
}
first = false;
}
else{
sb.append(S.charAt(i));
}
i++;
}
if(!vowel){
sb.append(firstC);
}
sb.append("ma");
for(int j=0;j<space;j++){
sb.append('a');
}
while(i< S.length() && S.charAt(i) == ' '){
i++;
}
if(i < S.length()){
sb.append(' ');
}
space++;
}
return sb.toString();
}
}