Skip to content

Latest commit

 

History

History
29 lines (25 loc) · 824 Bytes

Question_3442.md

File metadata and controls

29 lines (25 loc) · 824 Bytes

LeetCode Records - Question 3442 Maximum Difference Between Even and Odd Frequency I

Attempt 1: Use an int[] to store the character counts

class Solution {
    public int maxDifference(String s) {
        int[] counts = new int[26];
        for (char ch : s.toCharArray()) {
            counts[ch - 'a']++;
        }

        int minEvenCount = Integer.MAX_VALUE;
        int maxOddCount = Integer.MIN_VALUE;
        for (int count : counts) {
            if (count % 2 == 1) {
                maxOddCount = Math.max(maxOddCount, count);
            } else if (count > 0) {
                minEvenCount = Math.min(minEvenCount, count);
            }
        }

        return maxOddCount - minEvenCount;
    }
}
  • Runtime: 1 ms (Beats: 100.00%)
  • Memory: 42.37 MB (Beats: 92.25%)