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

#define all(x) x.begin(),x.end()
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define LL long long
#define LD long double
#define pb push_back
#define F first
#define S second


const double PI=3.1415926535897932384626433;
const int KL=1e6;
const LL MOD=1e9+7;


using namespace std;
int main ()
{
    string st1 ;
    cin >> st1 ;
    int a[30]={0} ;
    for (int i=0;i<st1.size();i++)
    {
        a[st1[i]-'a']++ ;
    }
    LL ans =0 ;
    LL h=1e9 ;
    for (int i=0;i<26;i++)
    {
        ans= 0 ;
        for (int n=0;n<26;n++)
        {
            if (i==n)continue ;
            ans+= abs(i-n)*a[n] ;
        }
       // cout << ans << "\n" ;
        h=min (h,ans) ;
    }
    cout << h << "\n" ;






}
Copy
Palindrome Substrings alialsuleman
GNU G++17
4 ms
836 KB
Accepted