Simple Java solution without HashMap


  • 0
    Y
    public class Solution {
        public int longestPalindrome(String s) {
        char[] word=new char[256];
        for(int i=0;i<s.length();i++)
        {
           char c=s.charAt(i);
           word[c]++;
        }
        int count=0;
        for(int i=0;i<word.length;i++)
        {
            count+=word[i]/2;
        }
        //find if we can have a single character in the middle
        return count*2==s.length()?2*count:2*count+1;
        }
    }
    

Log in to reply
 

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