Created
June 10, 2025 16:55
-
-
Save nags0x/32013853ff43b3c437972e73818fca3b to your computer and use it in GitHub Desktop.
#3442
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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