Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define vi vector<int>
#define vvi vector<vector<int,int>>
#define vll vector<ll>
#define sz(a) (int)a.size()
#define mp(x,y) make_pair(x,y)
#define read freopen("math.in","r",stdin);
#define fastinput ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
//int dx[] = { -1,-1,-1,0,1,1,1,0 }, dy[] = { -1,0,1,1,1,0,-1,-1 }; //8 neighbours moving
//int dx[] = { -1,0,1,0 }, dy[] = { 0,1,0,-1 }; //4 neighbours moving
const double PI = 3.14159265;
const auto EPS = 1e-9;
const ll N = 1e5 + 1, OO = 1e16, MOD = 1e9 + 7;

int main() 
{
	fastinput;
	string s;
	cin >> s;
	int n = s.length();
	int res = INT_MAX;
	for (char c = 'a'; c <= 'z'; c++)
	{
		int cost = 0;
		for (int i = 0; i < n; i++)
		{
			cost += abs(s[i] - c);
		}
		res = min(res, cost);
	}
	cout << res;
	return 0;
}
Copy
Palindrome Substrings Khedr
GNU G++17
3 ms
468 KB
Accepted