3442. Maximum Difference Between Even and Odd Frequency I
Intuition
The problem requires us to find the maximum difference between odd and even frequencies of characters in a string. We can solve this by counting the frequency of each character and then finding the maximum odd frequency and minimum even frequency.
Approach
- Create a frequency array
record
of size 26 to store the count of each lowercase letter - Count the frequency of each character in the string
- Iterate through the frequency array:
- Skip if frequency is 0
- For odd frequencies, update the maximum odd frequency
- For even frequencies, update the minimum even frequency
- Return the difference between maximum odd frequency and minimum even frequency
Complexity
- Time complexity: O(n)
- Space complexity: O(1)
Keywords
- Frequency Count
- Array
- Maximum Difference
Code
func maxDifference(s string) int {
record := make([]int, 26)
maxRecord, minRecord := math.MinInt, math.MaxInt
for _, c := range s {
record[c - 'a'] += 1
}
for _, v := range record {
if v == 0 {
continue
}
if v & 1 == 1 {
maxRecord = max(maxRecord, v)
} else {
minRecord = min(minRecord, v)
}
}
return maxRecord - minRecord
}