#include <iostream>
#include <string>
using namespace std;
int n, cnt = 1, ans = 0;
string s;
int main(){
cin >> n >> s;
s = '/'+s;
for (int i = 1; i <= n; ++i){
if (s[i]!=s[i-1]){
if (cnt==1) continue;
else {
int j = i;
for (int k = 1; k < cnt; ++k, --j)
swap(s[j], s[j-1]);
ans += --cnt;
}
} else {
cnt++;
}
}
if (cnt==2) ans++;
if (cnt>2) ans = -1;
cout << ans;
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... |