#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define sz size()
#define all(v) v.begin(),v.end()
#define fi first
#define se second
const int N = 1e5;
const int mod = 1e9+7;
const int INF = 1e9;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
int n;
int calc(string s){
int ans=0;
for(int i = 1; i <= n-1; i++){
if(s[i]==s[i+1]){
int pos=-1;
for(int j = i+1; j <= n; j++){
if(s[i]!=s[j]){
pos=j;
break;
}
}
for(int j = pos-1; j >= i+1; j--){
swap(s[j],s[j+1]);
ans++;
}
}
}
for(int i = 1; i <= n-1; i++){
if(s[i]==s[i+1]){
return INF;
}
}
return ans;
}
void solve() {
string s;
cin>>n>>s;
reverse(all(s));
string t=s;
reverse(all(s));
s="!"+s,t="!"+t;
int ans=min(calc(s),calc(t));
if(ans==INF)
ans=-1;
cout<<ans<<"\n";
}
int main() {
//freopen("cowrun.in","r",stdin);
//freopen("cowrun.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
//cin>>t;
while(t--) {
solve();
}
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... |