Concise Java solution.


  • 0
    X
    public int longestPalindrome(String s) {
        int[] count = new int[128];
        for (char a : s.toCharArray()) {
          count[a]++;
        }
        int max = 0;
        for (int i : count) {
          if (i > 1) {
            max += i / 2;
          }
        }
    
        return max * 2 + 1 > s.length() ? max * 2 : max * 2 + 1;
      }
    

Log in to reply
 

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