Source Code
#include<bits/stdc++.h>

#define ll long long
using namespace std;

ll NN=1e6+10;

int cnt[500];
int main()
{
    string s; cin >> s;
    int n = s.size();
    sort(s.begin(),s.end());
    //cout << s << endl;
    ll ans =0;
    if(n%2)
    {
        for(int i=0;i<n;i++)
        {
            ans+= abs((int) (s[i] - s[n/2]));
        }
        cout << ans << endl;
    }
    else
    {
        int ans1=0 , ans2=0;
        for(int i=0;i<n;i++)
        {
            ans1+= abs((int) (s[i] - s[n/2]));
        }
        for(int i=0;i<n;i++)
        {
            ans2+= abs((int) (s[i] - s[n/2 + 1]));
        }
        cout << min (ans1 , ans2);
    }


    return 0;
}
Copy
Palindrome Substrings Practice_Til_Red00
GNU G++17
7 ms
832 KB
Accepted