Longest Palindrome
给一个字符串, 找出其中最长的回文, 返回它的长度.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Solution { public int longestPalindrome(String s) { if(s == null || s.length() == 0) return 0; Set<Character> set = new HashSet<Character>(); int count = 0; for(int i = 0 ; i < s.length(); i++) { if(set.contains(s.charAt(i))){ set.remove(s.charAt(i)); count++; } else{ set.add(s.charAt(i)); } } if(!set.isEmpty()) return count*2+1; else return count*2; } } |