## Longest Palindrome(leetcode409) 原

woshixin

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example `"Aa"` is not considered a palindrome here.

Note:
Assume the length of given string will not exceed 1,010.

Example:

```Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.```
```//自然想到的正向思维 偶数的数量肯定可以组成回文，奇数只要获取其中的偶数个 如果可以中间还可以放一个
public static int longestPalindrome(String s) {

int result = 0;
Map<Character,Integer> map = new HashMap<>();
for (int i =0;i<s.length();i++){
if(map.containsKey(s.charAt(i))){
Integer count = map.get(s.charAt(i));
count ++;
map.put(s.charAt(i),count);
}else{
map.put(s.charAt(i),1);
}
}
int jishu =0;
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if(entry.getValue()%2==0){
result+=entry.getValue();
}else{
result = entry.getValue()+result-1;
jishu++;
}
}
if(jishu> 0){
result++;
}

return result;
}

//这个加加减减的方式 也是可以的
public static int longestPalindrome2(String s) {
if(s==null || s.length()==0) {
return 0;
}
HashSet<Character> hs = new HashSet<Character>();
int count = 0;
for(int i=0; i<s.length(); i++){
if(hs.contains(s.charAt(i))){
hs.remove(s.charAt(i));
count++;
}else{
}
}
if(!hs.isEmpty()) {
return count*2+1;
}
return count*2;
}

//这里用整型数量的特性 除以2乘以2  就没有奇数偶数的问题
public static int longestPalindrome3(String s) {
int[] lowercase = new int[26];
int[] uppercase = new int[26];
int res = 0;
for (int i = 0; i < s.length(); i++){
char temp = s.charAt(i);
if (temp >= 97) {
lowercase[temp-'a']++;
} else {
uppercase[temp-'A']++;
}
}
for (int i = 0; i < 26; i++){
res+=(lowercase[i]/2)*2;
res+=(uppercase[i]/2)*2;
}
return res == s.length() ? res : res+1;

}

//与上面的不同  这是获取奇数的数量
public static int longestPalindrome4(String s) {
int[] chars = new int[128];
char[] t = s.toCharArray();
for(char c:t){
chars[c]++;
}
int single = 0;
for(int n:chars){
if(n%2!=0){
single++;
}
}
return single>1?t.length-single+1:t.length;
}```

git:https://github.com/woshiyexinjie/leetcode-xin

### woshixin

[leetcode] Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. ht......

jdflyfly
2014/06/24
614
0
【LeetCode】409. Longest Palindrome （java实现）

BookShu
2016/11/04
145
0
[LeetCode] Shortest Palindrome 最短回文串

Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this tran......

2017/12/02
0
0
LeetCode Question Difficulty Distribution 问题难度和频率分布

Leetcode问题难度和频率分布表 引用自： https://zephyrusara.blogspot.jp/2014/07/leetcode-question-difficulty.html LeetCode Question Difficulty Distribution : Sheet1......

xidiancoder
2017/09/10
0
0
topcoder题目及我的程序（4）——find reversed string （算法1）

Problem Statement ???? You are given a String input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case......

2012/03/09
265
0

RabbitMQ入门

watermelon11

15
0

2
0

focusone

4
0
virtualbox中安装ubuntu

virtualbox+ubuntu 安装virtualbox，当前版本是6.0.4 下载ubuntu安装盘，建议lubuntu，链接是http://mirrors.ustc.edu.cn/ubuntu-cdimage/lubuntu/releases/18.04.2/release/lubuntu-18.04.......

chuqq

5
0
exists 谓词的子查询

https://blog.csdn.net/qq_19782019/article/details/78730882

5
0