#409.Longest Palindrome [LeetCode Grind 75 in Java]

[Problem Link] https://leetcode.com/problems/longest-palindrome/

class Solution {
    public int longestPalindrome(String s) {
        int[] count = new int[128];

        for(char c : s.toCharArray()) count[c] ++;

        int ans = 0;
        boolean onef = false;

        for(int freq : count){
            //even frequencies will all be added
            if(freq % 2 == 0) ans += freq;
            //odd frequencies > 1 = 3,5,7...
            else if (freq != 1) {
                ans += freq - 1; 
                onef = true;
            }else onef = true; //frequency = 1
        }

        //add 1 for all the characters who have frequency = 1
        if(onef) ans ++;
        return ans;
    }
}