-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path767_Reorganize_String.cpp
More file actions
93 lines (88 loc) · 2.24 KB
/
767_Reorganize_String.cpp
File metadata and controls
93 lines (88 loc) · 2.24 KB
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
#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
#define ll long long
#define ull unsigned long long
#define readi(x) int x; cin >> x
#define readll(x) ll x; cin >> x
#define reads(s) string s; cin >> s
#define rep(a, b) for (ll i = a; i < b; ++i)
#define repi(i, a, b) for (ll i = a; i < b; ++i)
#define repd(i, a, b) for (ll i = a; i > b; --i)
#define all(x) (x).begin(), (x).end()
#define init_arr readll(n); vll arr(n); rep(0, n) cin >> arr[i];
#define vprint(i) for (auto &j : i) cout << j << ' '; nl
#define mpprint(mp) for (auto &i : mp) { cout << i.ff << ' ' << i.ss << '\n'; }
#define vvprint(arr) for (auto &i : arr) { vprint(i) }
#define vvcin(n, m, arr) repi(i, 0, n) repi(j, 0, m) cin >> arr[i][j];
#define vcin(n, arr) rep(0, n) cin >> arr[i];
#define umcll unordered_map<char, ll >
#define umsll unordered_map<string, ll >
#define umll unordered_map<ll, ll >
#define print(num) cout << num << '\n';
#define mpcll map<char, ll >
#define mpsll map<string, ll >
#define mpll map<ll, ll >
#define nl cout << '\n';
#define inf (1LL << 60)
#define vll vector<ll >
#define vi vector<int >
#define vii vector<vi >
#define vs vector<string >
#define vss vector<vs >
#define vb vector<bool >
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mod 1e9 + 7
#define ff first
#define ss second
#define N 1e6
class Solution {
public:
string reorganizeString(string s) {
priority_queue<pair<int, char>> maxh;
unordered_map<char, int> map;
int n = s.size();
if (n < 0) return "";
for (int i = 0; i < n; ++i)
{
map[s[i]]++;
}
for (auto &it : map)
{
maxh.push({it.second, it.first});
}
string res = "";
pair<int, char> block = maxh.top();
maxh.pop();
while (!maxh.empty())
{
pair<int, char> temp = maxh.top();
maxh.pop();
res.push_back(block.second);
block.first -= 1;
if (block.first > 0)
{
maxh.push({block.first, block.second});
}
block = temp;
}
if (block.first == 1)
res.push_back(block.second);
else if (block.first > 1)
return "";
return res;
}
};
int main() {
fast_io;
ll t = 1;
// cin >> t;
while (t--) {
Solution ob;
reads(s);
cout << ob.reorganizeString(s) << '\n';
}
return 0;
}