long but easy to understand


  • 0
    J
    public class Solution {
    	public int longestPalindrome(String s) {
    		HashMap<Character,Integer> map = new HashMap();
    		for(int i = 0;i<s.length();i++){
    			if(map.get(s.charAt(i))!=null){
    				map.put(s.charAt(i),map.get(s.charAt(i))+1);
    			}
    			else{
    				map.put(s.charAt(i),1);
    			}
    		}
    		int max = 0;
    		int star = 0;
    		for(Character x : map.keySet()){
    			if(map.get(x)%2==0){
    				max+=map.get(x);
    			}
    			else{
    				star++;
    				max+=(map.get(x)-1);
    			}
    		}
    		if(star>0){
    			max+=1;
    		}
    		return max;
    	}
    }
    

Log in to reply
 

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