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.

给一个含有大写和小写字母的字符串, 找出能用这些字母组成的最长的回文。大小写敏感,字符长度不超过1010。

解法:由于字母可以随意组合,求出字符个数为偶数的字母,回文有两种形式,一种是左右完全对称,还有一种是以中间字符为中心,左右对称,统计出所有偶数个字符的出现总和,如果有奇数个字符的话,最后结果再加1。

Java:

public int longestPalindrome(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{
hs.add(s.charAt(i));
}
}
if(!hs.isEmpty()) return count*2+1;
return count*2;
} 

Python:

class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: int
"""
ans = odd = 0
cnt = collections.Counter(s)
for c in cnt:
ans += cnt[c]
if cnt[c] % 2 == 1:
ans -= 1
odd += 1
return ans + (odd > 0)

Python:

class Solution(object):
def longestPalindrome(self, s):
map = {}
for i in s:
if i in map:
map[i] += 1
else:
map[i] = 1 odd, even = 0, 0
for key in map:
if map[key] % 2:
odd += 1
else:
even += 1 return even + 1 if odd else even

Python:

import collections

class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: int
"""
odds = 0
for k, v in collections.Counter(s).iteritems():
odds += v & 1
return len(s) - odds + int(odds > 0) def longestPalindrome2(self, s):
"""
:type s: str
:rtype: int
"""
odd = sum(map(lambda x: x & 1, collections.Counter(s).values()))
return len(s) - odd + int(odd > 0)

C++:

class Solution {
public:
int longestPalindrome(string s) {
int odds = 0;
for (auto c = 'A'; c <= 'z'; ++c) {
odds += count(s.cbegin(), s.cend(), c) & 1;
}
return s.length() - odds + (odds > 0);
}
};

  

  

类似题目:

[LeetCode] 5. Longest Palindromic Substring 最长回文子串

[LeetCode] 125. Valid Palindrome 有效回文

[LeetCode] 9. Palindrome Number 验证回文数字

[LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

  

All LeetCode Questions List 题目汇总

[LeetCode] 409. Longest Palindrome 最长回文的更多相关文章

  1. 409 Longest Palindrome 最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...

  2. leetcode 5 Longest Palindromic Substring--最长回文字符串

    问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  3. Longest Palindrome 最长回文串问题

    1.题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...

  4. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  5. 24. leetcode 409. Longest Palindrome

    409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...

  6. LeetCode 409 Longest Palindrome

    Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...

  7. LeetCode之“字符串”:最长回文子串

    题目要求: 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串.例如,给出字符串 "abcdzdcab",它的最长回文子串为 & ...

  8. LeetCode——409. Longest Palindrome

    题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...

  9. 转载:LeetCode:5Longest Palindromic Substring 最长回文子串

    本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...

随机推荐

  1. MySQL:查询、修改(二)

    干货: 使用SELECT查询的基本语句SELECT * FROM <表名>可以查询一个表的所有行和所有列的数据.SELECT查询的结果是一个二维表. 使用SELECT *表示查询表的所有列 ...

  2. python通过json读写序列类型的数据文件

    import json class a: def writeReadJson(self): list2 =['] with open("test.txt",'w') as f: j ...

  3. 微信小程序~页面跳转和路由

    一个小程序拥有多个页面,我们可以通过wx.navigateTo推入一个新的页面,如图3-6所示,在首页使用2次wx.navigateTo后,页面层级会有三层,我们把这样的一个页面层级称为页面栈.

  4. node 日志 log4js 错误日志记录

    SET DEBUG=mylog:* & npm start 原文出处:http://blog.fens.me/nodejs-log4js/ 1. 默认的控制台输出 我们使用express框架时 ...

  5. Touch事件 移动端touch触摸事件

    <!-- HTML5 --> <!DOCTYPE html> <html> <head> <title>TouchEvent测试</t ...

  6. mongodb 简单使用说明

    首先安装  mongodb软件地址 https://www.mongodb.org/downloads#production: 然后在 mongodb安装目录下找到bin 文件夹进去 在它的位置上按下 ...

  7. janusgraph单机版安装

    注:本次安装janusgraph基于es和hbse,所以先安装es和hbase 1.安装jdk 2.安装janusgraph 解压安装文件至/usr/janusgraph-0.3.1 unzip ja ...

  8. Making Huge Palindromes LightOJ - 1258

    题目链接:LightOJ - 1258 1258 - Making Huge Palindromes   PDF (English) Statistics Forum Time Limit: 1 se ...

  9. Optimal Marks SPOJ - OPTM(最小割)

    传送门 论文<最小割模型在信息学竞赛中的应用>原题 二进制不同位上互不影响,那么就按位跑网络流 每一位上,确定的点值为1的与S连一条容量为INF的有向边.为0的与T连一条容量为INF的有向 ...

  10. 09-Flutter移动电商实战-移动商城数据请求实战

    1.URL接口管理文件建立 第一步需要在建立一个URL的管理文件,因为课程的接口会一直进行变化,所以单独拿出来会非常方便变化接口.当然工作中的URL管理也是需要这样配置的,以为我们会不断的切换好几个服 ...