3442: Maximum-Difference-Between-Even-and-Odd-Frequency-I
Easy
table of contents
This question is like quite straightforward I reckon.
Just iterate through the string s
, keeping track of each
letter’s frequency as you iterate through it using a
hashmap.
Afterwards, just iterate through all the letters and find the:
Then, simply return maxOdd - minEven
as the
solution.
code
class Solution {
public:
int maxDifference(string s) {
int alphabetFrequency[26] = {0};
for (char c : s) {
++alphabetFrequency[c-'a'];
}
int minEven = INT32_MAX;
int maxOdd = 0;
for (int i = 0; i < 26; ++i) {
if (alphabetFrequency[i] != 0) {
if (alphabetFrequency[i] % 2 == 0) {
= min(minEven, alphabetFrequency[i]);
minEven } else {
= max(maxOdd, alphabetFrequency[i]);
maxOdd }
}
}
return maxOdd - minEven;
}
};