#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 ll INF = 1e18;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
void solve() {
int n;
cin>>n;
string s;
cin>>s;
s="!"+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]){
cout<<"-1\n";
return;
}
}
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... |