Advertisement
Guest User

Untitled

a guest
May 25th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. class Solution {
  2. public:
  3. string reorganizeString(string s) {
  4. int count[26] = {};
  5. int max_count = 0;
  6. char max_char = 0;
  7. for (char c : s) {
  8. count[c - 'a']++;
  9. if (count[c - 'a'] > max_count) {
  10. max_count = count[c - 'a'];
  11. max_char = c;
  12. }
  13. }
  14. if (max_count > ceil((double)s.size() / 2)) {
  15. return "";
  16. }
  17. int k = 0;
  18. char ca[s.size() + 1] = {};
  19. for (int j = 0; j < count[max_char - 'a']; ++j) {
  20. ca[k] = max_char;
  21. k += 2;
  22. }
  23. if (k >= s.size()) {
  24. k = 1;
  25. }
  26. for (int i = 0; i < 26; ++i) {
  27. if (i != max_char - 'a') {
  28. for (int j = 0; j < count[i]; ++j) {
  29. ca[k] = 'a' + i;
  30. k += 2;
  31. if (k >= s.size()) {
  32. k = 1;
  33. }
  34. }
  35. }
  36. }
  37. ca[s.size()] = 0;
  38. string res(ca);
  39. return res;
  40. }
  41. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement