My java solution


  • 0
    C
     import java.util.*;
     import java.util.Map.Entry;
     public class Solution {
      public int longestPalindrome(String s) {
        int sum = 0;
    	Hashtable<Character,Integer> table = new Hashtable();
    	for(int i=0;i<s.length();i++)
    	{
    		Integer count = table.get(s.charAt(i));
    		if(count != null)
    		table.put(s.charAt(i),++count);
    		else
    			table.put(s.charAt(i), 1);
    	}
    	if(table.size() == 1)
    		return s.length();
    	Set<Entry<Character, Integer>> entrySet = table.entrySet();
    	boolean v = true;
    	for(Entry<Character,Integer> entry: entrySet)
    	{	int tmp = 0;
    		tmp = entry.getValue();
    		if(tmp%2 == 0)
    		{
    			sum+=tmp;
    		}
    		else if(tmp >2)
    		{
    			if(v == true)
    			{
    				sum+=tmp;
    				v= false;
    			}
    			else
    			{
    				sum+=tmp-1;
    			}
    		}
    		else if((tmp == 1) &&(v == true))
    		{
    			sum++;
    			v = false;
    		}
    	}
    	return sum;
    }
    

    }


Log in to reply
 

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