-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFG Alternate Vowel and Consonant String
65 lines (47 loc) · 1.47 KB
/
GFG Alternate Vowel and Consonant String
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
class Solution{
public:
string rearrange (string s, int n)
{
vector<int> dictc(26,0), dictv(26,0);
int l1=0,l2=0;
char minc='z'+1,minv='z'+1;
for(char ch: s)
{
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ){
minv = min(minv, ch);
dictv[ch-'a']++;l1++;
}
else{
minc = min(minc, ch);
dictc[ch-'a']++;l2++;
}
}
int i1=0,i2=0;
string ans="";
if(abs(l1-l2) >1) return "-1";
bool takev = false;
if(l1>l2) takev = true;
else if(l2==l1 && minv < minc) takev = true;
while(i1<26 && i2<26){
if(takev){
while(dictv[i1] ==0) i1++;
if(i1>=26) break;
ans += (i1+'a');
dictv[i1]--;
takev = false;
}
else{
while(dictc[i2]==0) i2++;
if(i2>=26) break;
ans += (i2+'a');
dictc[i2]--;
takev = true;
}
}
while(dictv[i1]==0) i1++;
while( dictc[i2]==0) i2++;
if(i1<26) ans += (i1+'a');
else if(i2<26) ans += (i2+'a');
return ans;
}
};