Java beats 88% easy understand solution


  • 0
    O
    public int longestPalindrome(String s) {
            int len = s.length();
            if(len == 0) return 0;
            int[] chs = new int[256];
            for(int i = 0; i < len; i++){
                char c = s.charAt(i);
                chs[c]++;
            }
            
            int res = 0;
            for(int i = 0; i < 256; i++){
                if(chs[i] != 0){
                    //always add even value
                    if(chs[i] % 2 == 0){
                        res += chs[i];
                    }else{
                        res += (chs[i]-1);
                    }
                }
            }
            // if the entire string is even length palindrome
            if(res == len) return res;
            else return res+1;
        }
    

Log in to reply
 

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