Skip to content

Instantly share code, notes, and snippets.

@nags0x
Created June 10, 2025 16:55
Show Gist options
  • Select an option

  • Save nags0x/32013853ff43b3c437972e73818fca3b to your computer and use it in GitHub Desktop.

Select an option

Save nags0x/32013853ff43b3c437972e73818fca3b to your computer and use it in GitHub Desktop.
#3442
#include <iostream>
#include <unordered_map>
#include <climits>
#include <string>
using namespace std;
int maxDiffEvenOddFreq(const string& s) {
unordered_map<char, int> freq;
// Count frequency of each character
for (char ch : s) {
freq[ch]++;
}
int maxDiff = INT_MIN;
// Compare every odd freq with every even freq — no need to store separately
for (auto& [ch1, count1] : freq) {
if (count1 % 2 == 1) { // odd
for (auto& [ch2, count2] : freq) {
if (count2 % 2 == 0) { // even
maxDiff = max(maxDiff, count1 - count2);
}
}
}
}
return maxDiff;
}
int main() {
string s1 = "aaaaabbc";
string s2 = "abcabcab";
cout << maxDiffEvenOddFreq(s1) << endl; // Output: 3
cout << maxDiffEvenOddFreq(s2) << endl; // Output: 1
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment