-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2047B.cpp
More file actions
46 lines (38 loc) · 1019 Bytes
/
2047B.cpp
File metadata and controls
46 lines (38 loc) · 1019 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--) {
int n; cin >> n;
string s; cin >> s;
map<char, int> freq;
for (char ch : s) {
freq[ch]++;
}
// Find character with max and min frequency
char maxChar = s[0], minChar = s[0];
int maxFreq = 0, minFreq = INT_MAX;
for (auto &[ch, count] : freq) {
if (count > maxFreq) {
maxFreq = count;
maxChar = ch;
}
if (count <= minFreq ) {
minFreq = count;
minChar = ch;
}
}
// Replace the first occurrence of minChar with maxChar
rep(i, n) {
if (s[i] == minChar) {
s[i] = maxChar;
break;
}
}
cout << s << "\n";
}
return 0;
}