leetcode409

package com.problem01;

import java.util.HashMap;


public class Main {

	public static void main(String[] args) {
		String s = "abcddbcaa";
		System.out.println(longestPalindrome(s));
	}
	
	 public static int longestPalindrome(String s) {
	        int maxlength=0;
	        int flag = 0;
	        
	        HashMap<Character, Integer> map = new HashMap<>();
	        
	        for(int i = 0; i < s.length(); i++) {
	        	if(map.containsKey(s.charAt(i))) {
	        		map.put(s.charAt(i), map.get(s.charAt(i))+1);
	        	} else {
	        		map.put(s.charAt(i), 1);
	        	}
	        }
	        
	        for(Character c : map.keySet()) {
	        	if(map.get(c) % 2 == 0) {
	        		maxlength = maxlength + map.get(c);
	        	} else {
	        		maxlength = maxlength + map.get(c) - 1;
	        		flag = 1;
	        	}
	        }
	
	        return maxlength+flag;
	}

}

  

原文地址:https://www.cnblogs.com/wylwyl/p/10720683.html