Source Code
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int o[26] = {}, ans = 2e9;
    string s;
    cin >> s;

    for (char c : s)
        o[c - 'a']++;

    for (int i = 0; i < 26; i++) {
        int cur = 0;
        for (int j = 0; j < 26; j++)
            cur += abs(i - j) * o[j];
        ans = min(ans, cur);
    }
    cout << ans << '\n';
}
Copy
Palindrome Substrings ahmad_salah
GNU G++17
3 ms
944 KB
Accepted