Source Code
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <iomanip>
#include <cmath>
#include <cassert>
using namespace std;

#define all(x) x.begin(), x.end()
#define pb push_back
#define sz(a) ((int) a.size())

using ll = long long;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed;
    cout.precision(6);
    string s;
    cin >> s;
    int ans = 1e9;

    for (char i = 'a'; i <= 'z'; i++) {
        int cost = 0;
        for (int j = 0; j < s.size(); j++) {
            cost += abs(s[j] - i);
        }
        ans = min(ans, cost);
    }
    cout << ans;


    return 0;
}
Copy
Palindrome Substrings MohamedMagdy
GNU G++17
5 ms
848 KB
Accepted