Skip to content

Instantly share code, notes, and snippets.

@Chen-tao
Created February 6, 2017 15:31
Show Gist options
  • Save Chen-tao/9207351ba7a9dcac9b19b25699020ee3 to your computer and use it in GitHub Desktop.
Save Chen-tao/9207351ba7a9dcac9b19b25699020ee3 to your computer and use it in GitHub Desktop.
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