very simple Java solution use boolean[]


  • 4
    S
    public int longestPalindrome(String s) {
        boolean[] set = new boolean[256];
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            count += set[s.charAt(i)]?2:0;
            set[s.charAt(i)] = !set[s.charAt(i)];
        }
        if (count < s.length()) count++;
        return count;
    }

Log in to reply
 

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