#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int R = 0, G = 0, Y = 0;
for (char c : s) {
if (c == 'R') R++;
else if (c == 'G') G++;
else if (c == 'Y') Y++;
}
if (R > n/3 || G>n/3 || Y>n/3) {
cout << -1 << endl;
return 0;
}
int ans = 0;
for (int i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
for (int j = i + 1; j < n; j++) {
if (s[j] != s[i]) {
swap(s[i + 1], s[j]);
ans += (j - (i + 1));
break;
}
}
}
}
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |