Created
February 6, 2017 15:31
-
-
Save Chen-tao/9207351ba7a9dcac9b19b25699020ee3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int longestPalindrome(String s) { | |
int m = 0; | |
int n = 0; | |
Map<Character, Integer> cache = new HashMap<Character, Integer>(); | |
char[] sc = s.toCharArray(); | |
for(char c : sc){ | |
if(cache.get(c) != null){ | |
cache.put(c, cache.get(c) + 1); | |
} else { | |
cache.put(c, 1); | |
} | |
} | |
for(char c : cache.keySet()){ | |
int count = cache.get(c); | |
m += count / 2; | |
if(count%2 == 1){ | |
++n; | |
} | |
} | |
if(n > 1){ | |
n = 1; | |
} | |
return m*2+n; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment