Easy to understand Java Solution


  • 0
    E
    public class Solution {
        public int longestPalindrome(String s) {
            int arr[]=new int[256];
            for(char ch:s.toCharArray()){
                arr[ch]++;
            }
            int len=0,flag=0;
            for(int i=0;i<256;i++){
                if(arr[i]%2==0){
                    len+=arr[i];
                }
                else {
                    len+=arr[i]-1;
                    flag=1;
                }
            }
            return flag==0?len:len+1;
        }
    }
    

Log in to reply
 

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