|
| 1 | +You are given a string s consisting of lowercase English letters. |
| 2 | + |
| 3 | +Your task is to find the maximum difference diff = a1 - a2 between the frequency of characters a1 and a2 in the string such that: |
| 4 | + |
| 5 | +a1 has an odd frequency in the string. |
| 6 | +a2 has an even frequency in the string. |
| 7 | +Return this maximum difference. |
| 8 | +----------------------------------------------------- |
| 9 | + |
| 10 | + class Solution: |
| 11 | + def maxDifference(self, s: str) -> int: |
| 12 | + freq = dict(Counter(s)) |
| 13 | + |
| 14 | + freq = [[k,v] for k,v in freq.items()] |
| 15 | + freq.sort(key = lambda x: x[1]) |
| 16 | + |
| 17 | + freq = [v for k,v in freq] |
| 18 | + max_odd = max(list(filter(lambda x: x%2 == 1,freq))) |
| 19 | + |
| 20 | + min_ev = min(list(filter(lambda x: x%2 == 0, freq))) |
| 21 | + return max_odd-min_ev |
0 commit comments