fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int t;
  6. cin >> t;
  7. while (t--){
  8. string s;
  9. int length;
  10. cin >> length >> s;
  11. unordered_map<char, int> hash;
  12. for(int i = 0; i < s.length(); i++){
  13. hash[s[i]]++;
  14. }
  15. int max_count = 0, res = -1;
  16. int min_count = 11, tres = -1;
  17. for (auto i : hash) {
  18. if (max_count < i.second) {
  19. res = i.first;
  20. max_count = i.second;
  21. }
  22. if (min_count > i.second) {
  23. tres = i.first;
  24. min_count = i.second;
  25. }
  26. }
  27. if (max_count == min_count){
  28. res = s[0];
  29. for(int j = 1; j < s.length(); j++){
  30. if(s[j] != res){
  31. s[j] = res;
  32. break;
  33. }
  34. }
  35.  
  36. }
  37. else{
  38. size_t pos = s.find(tres);
  39. s[pos] = char(res);
  40. }
  41.  
  42. cout << s << endl;
  43. }
  44.  
  45. }
Success #stdin #stdout 0.01s 5288KB
stdin
6
3
abc
4
xyyx
8
alphabet
1
k
10
aabbccddee
6
ttbddq
stdout
aac
xxyx
alphabea
k
aaabccddee
ttbddd