-
Notifications
You must be signed in to change notification settings - Fork 69
/
Reorganize String #79.txt
67 lines (59 loc) Β· 1.04 KB
/
Reorganize String #79.txt
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
#include <bits/stdc++.h>
using namespace std;
string reorganize(string str)
{
unordered_map<char, int> mp;
int mx=0;
char ch;
for(char c:str)
{
mp[c]++;
if(mx<mp[c])
{
mx=mp[c];
ch=c;
}
}
int n=str.size();
vector<char> v(n);
int i=0;
while(mx--)
{
if(i>=n)
{
return "''";
}
v[i]=ch;
i+=2;
}
for(auto it:mp)
{
if(it.first!=ch)
{
int k=it.second;
while(k--)
{
if(i>=n)
{
i=1;
}
v[i]=it.first;
i+=2;
}
}
}
string ans = "";
for(int i=0; i<n; i++)
{
ans+=v[i];
}
return ans;
}
int main()
{
string str;
cout<<"\nEnter a string : "<<endl;
cin>>str;
cout<<"\nOutput : "<<reorganize(str)<<endl;
return 0;
}