JAVA Solution beating 84.78%, easy to understand


  • 0
    T

    public class Solution {
    public int longestPalindrome(String s) {
    if(s==null)return 0;
    int []array=new int[58];
    int res=0;
    boolean flag=false;//Decide if we need to plus 1 with res
    for(int i=0;i<s.length();i++)
    array[s.charAt(i)-'A']++;
    for(int i:array){
    if(i%2==0)
    res+=i;
    else{
    res+=i-1;
    flag=true;//If there are characters that appear odd number of time, res plus 1
    }
    }
    return flag==true? res+1:res;
    }
    }


Log in to reply
 

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