Java 10 lines one-pass


  • 0
    public int longestPalindrome(String s) {
        int[] counter = new int[128];
        int maxLen = 0;
        for(char c:s.toCharArray()){
            ++counter[c];
        }
        for(int n:counter){
            maxLen += n - (maxLen & n & 1);
        }
        return maxLen;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.