Very simple solution use one array


  • 0
    Y

    public class Solution {
    public int longestPalindrome(String s) {
    if(s == null || s.length() == 0){
    return 0;
    }
    int [] count = new int[128];
    for(char ch : s.toCharArray()){
    count[ch - 'A']++;
    }
    int sum = 0;
    boolean flag = false;
    for(int i = 0; i < 128; i++){
    if(count[i]%2 == 0){
    sum += count[i];
    }else{
    sum += count[i] - 1;
    flag = true;
    }
    }
    return flag == true? sum + 1: sum;
    }
    }


Log in to reply
 

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