Source Code
#include <iostream>
#include <algorithm>
#include <iterator>
#include <math.h>
#include <vector>
#include <set>
#include <map>
using namespace std;
int main() {
	string s;
	long long n = 1e9, c = 0;
	cin >> s;
	int f[27] = { 0 };
	for (int i = 0; i < s.size(); i++) {
		f[s[i] - 'a']++;
	}
	for (int i = 0; i < 26; i++) {
		c = 0;
		for (int j = 0; j < 26; j++) {
			if (i == j)continue;
			else {
				c += (f[j] * abs(j - i));
			}
		}
		n = min(n, c);
	}
	cout << n;
	return 0;
}
Copy
Palindrome Substrings Sarah
GNU G++17
1 ms
912 KB
Accepted