-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec4_sortVowels.cpp
55 lines (51 loc) Β· 1.28 KB
/
lec4_sortVowels.cpp
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
https://leetcode.com/problems/sort-vowels-in-a-string/submissions/1297978511/
class Solution {
public:
string sortVowels(string s) {
vector<int>lower( 26 , 0);
vector<int>upper( 26 , 0);
for(int i = 0 ; i<s.size(); i++)
{
if(s[i] == 'a' || s[i] == 'e'|| s[i] == 'i'|| s[i] == 'o' || s[i] == 'u' )
{
lower[s[i] - 'a']++;
s[i]= '#';
}
else if(s[i] == 'A' || s[i] == 'E'|| s[i] == 'I'|| s[i] == 'O' || s[i] == 'U' )
{
upper[s[i] - 'A']++;
s[i]= '#';
}
}
string ans;
for(int i = 0 ; i<26; i++)
{
char c = 'A' + i ;
while(upper[i])
{
ans+=c ;
upper[i]--;
}
}
for(int i = 0 ; i<26; i++)
{
char c = 'a' + i ;
while(lower[i])
{
ans+=c ;
lower[i]--;
}
}
int first = 0 , second = 0 ;
while(second<ans.size())
{
if(s[first] == '#')
{
s[first] = ans[second];
second++;
}
first++;
}
return s;
}
};