-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path767.cpp
More file actions
31 lines (31 loc) · 752 Bytes
/
767.cpp
File metadata and controls
31 lines (31 loc) · 752 Bytes
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
class Solution {
public:
string reorganizeString(string S) {
int n = S.size();
if(n<2)
return S;
int max = (n+1)/2;
vector<int> count(26,0);
for(auto& ch:S){
count[ch-'a']+=1000;
}
for(int i=0;i<26;i++)
count[i] += i;
string res(n,0);
sort(count.begin(),count.end());
int t = 1;
for(int i =0; i<26;i++){
int cnt = count[i]/1000;
char ch = char('a' + count[i]%1000);
if(cnt>max)
return "";
while(cnt--){
if(t>=n)
t = 0;
res[t] = ch;
t += 2;
}
}
return res;
}
};