Please could someone help me with this
This is Arranging Books a part of the Junior CCC Contest 2020
My Code:
It fails duplicates - “SSLL”
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
string s;
cin>>s;
ll n = s.size();
if(n==0){
return 0;
}
ll arr[s.size()];
ll temp[s.size()];
for(ll i=0;i<s.size();i++){
if(s[i]=='L'){
arr[i] = 1;
temp[i] = 1;
}
else if(s[i]=='M'){
arr[i] = 2;
temp[i] = 2;
}
else{
arr[i] = 3;
temp[i] = 3;
}
}
ll ans = 0;
map<ll,ll>h;
sort(temp,temp+n);
for(ll i=0;i<n;i++){
h[arr[i]] = i;
}
for (ll i = 0; i < n; i++){
if (arr[i] != temp[i])
{
ans++;
ll init = arr[i];
swap(arr[i], arr[h[temp[i]]]);
h[init] = h[temp[i]];
h[temp[i]] = i;
}
}
cout<<ans<<"\n";
}