Solution by count number of appearance of each character

  • 0
    public class Solution {
        public int longestPalindrome(String s) {
             if(s==null||s.length()==0) return 0;
            if(s.length()==1) return 1;
            int[] charNums=new int[52];
            for(char i:s.toCharArray()){
            		charNums[i-'a']++;  //0-25 is lowercase
            		charNums[26+i-'A']++; //26-51 for uppercase
            int onlyone=0;
            int sum=0;
            for(int i : charNums){
            	if(onlyone==0&&i%2==1) onlyone=1;
            return (onlyone==1)?sum*2+1:sum*2;

Log in to reply

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