diff options
author | Jack Sangdahl <[email protected]> | 2025-06-09 22:59:08 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-06-09 22:59:08 -0600 |
commit | 766452a24801294dd5558c1f8e566b232e09d8e3 (patch) | |
tree | b6f50abf29523759211249d382355652c86b57d6 /max_diff_even_odd_freq1.py | |
parent | 27229aa885c8e82f4118905da553565136d6a71f (diff) | |
download | leetcode-766452a24801294dd5558c1f8e566b232e09d8e3.tar.gz leetcode-766452a24801294dd5558c1f8e566b232e09d8e3.zip |
3442. maximum difference between even and odd frequency 1
Diffstat (limited to 'max_diff_even_odd_freq1.py')
-rw-r--r-- | max_diff_even_odd_freq1.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/max_diff_even_odd_freq1.py b/max_diff_even_odd_freq1.py new file mode 100644 index 0000000..b1d6ad3 --- /dev/null +++ b/max_diff_even_odd_freq1.py @@ -0,0 +1,33 @@ +# 3442. Maximum Difference Between Even and Odd Frequency I + +""" +you are given a string 's' consisting of lowercase english letters. your task +is to find the minimum difference 'diff = a1 - a2' between the frequency of +characters 'a1' and 'a2' in the string such that 'a1' has an odd frequency in +the string, and 'a2' has an even frequency in the string. return this maximum +difference. +""" + + +class Solution(object): + def maxDifference(self, s): + """ + :type s: str + :rtype: int + """ + freq = [0] * 26 + for i in s: + freq[ord(i) - ord("a")] += 1 + maxi, mini = 0, len(s) + for i in range(26): + if freq[i] % 2 != 0: + maxi = max(maxi, freq[i]) + if freq[i] % 2 == 0 and freq[i] > 0: + mini = min(mini, freq[i]) + return maxi - mini + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxDifference(s="aaaaabbc")) + print(obj.maxDifference(s="abcabcab")) |