3442. Maximum Difference Between Even and Odd Frequency I
You are given a string
sconsisting of lowercase English letters.Your task is to find the maximum difference
diff = freq(a1) - freq(a2)between the frequency of charactersa1anda2in the string such that:
a1has an odd frequency in the string.a2has an even frequency in the string.Return this maximum difference.
Example 1:
Input: s = "aaaaabbc"
Output: 3
Explanation:
- The character
'a'has an odd frequency of5, and'b'has an even frequency of2.- The maximum difference is
5 - 2 = 3.Example 2:
Input: s = "abcabcab"
Output: 1
Explanation:
- The character
'a'has an odd frequency of3, and'c'has an even frequency of 2.- The maximum difference is
3 - 2 = 1.
def maxDifference(self, s: str) -> int:
d = {}
for ch in s:
if ch not in d:
d[ch] = 1
else:
d[ch] += 1
even , odd = [], []
for i in d.values():
if i % 2 == 0:
even.append(i)
else:
odd.append(i)
return max(odd) - min(even)