You are viewing a single comment's thread. Return to all comments →
C++ Solution
void countLetters(vector<int> &letters, string s, int begin, int end) { for (int i = begin; i < end; ++i) ++letters[s[i] - 'a']; } int numChanges(const string &s) { int length = s.length(); if (length % 2) // odd return -1; int half = length / 2; vector<int> v1(26, 0), v2(26, 0), results(26, 0); countLetters(v1, s, 0, half); countLetters(v2, s, half, length); transform (v1.begin(), v1.end(), v2.begin(), results.begin(), [](int x, int y) { return abs(x - y); }); int sum = accumulate(results.begin(), results.end(), 0); return sum / 2; }
Seems like cookies are disabled on this browser, please enable them to open this website
Anagram
You are viewing a single comment's thread. Return to all comments →
C++ Solution