-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAlternate Vowel and Consonant String.java
105 lines (92 loc) · 2.93 KB
/
Alternate Vowel and Consonant String.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class Solution{
public String rearrange(String S, int N){
int vowels = countVowels(S, true);
int consonants = countVowels(S, false);
int f[]=new int[26];
char firstVowel=Character.MAX_VALUE;
char firstConsonant=Character.MAX_VALUE;
for(int i=0;i<S.length();i++) {
f[S.charAt(i)-'a'] += 1;
if(S.charAt(i)=='a' || S.charAt(i)=='e' || S.charAt(i)=='i'|| S.charAt(i)=='o' || S.charAt(i)=='u') {
if(firstVowel > S.charAt(i)) {
firstVowel = S.charAt(i);
}
}
else {
if(firstConsonant > S.charAt(i)) {
firstConsonant = S.charAt(i);
}
}
}
int j=firstConsonant -'a';
int i=firstVowel -'a';
StringBuilder sb = new StringBuilder();
if(S.length()%2==0) {
if(vowels!=consonants) return "-1";
if(firstVowel < firstConsonant) {
sb.append(firstVowel);
f[firstVowel - 'a']-=1;
}
return getRes(f, i, j, sb);
}
else {
if(Math.abs(vowels-consonants) > 1) {
return "-1";
}
if(vowels-consonants==1) {
sb.append(firstVowel);
f[firstVowel - 'a']-=1;
}
return getRes(f, i, j, sb);
}
}
public String getRes(int []f, int i, int j, StringBuilder sb) {
while(j<26) {
if(f[j]==0) {
j+=1;
continue;
}
else {
if(j==0 || j==4 || j==8 || j==14 || j==20) {
j+=1;
continue;
}
else {
sb.append((char)('a'+j));
f[j]-=1;
}
}
if(f[i]==0) {
i=getNextVowel(f);
}
if(i!=-1) {
sb.append((char)('a'+i));
f[i]-=1;
}
else {
break;
}
}
return sb.toString();
}
public int getNextVowel(int f[]) {
if(f[0]!=0) return 0;
if(f[4]!=0) return 4;
if(f[8]!=0) return 8;
if(f[14]!=0) return 14;
if(f[20]!=0) return 20;
return -1;
}
public int countVowels(String s, boolean isVowel) {
int count = 0;
for(int i=0;i<s.length();i++) {
if(isVowel && (s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i'|| s.charAt(i)=='o' || s.charAt(i)=='u')) {
count+=1;
}
else if(!isVowel && !(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')) {
count+=1;
}
}
return count;
}
}