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.

这又是一道关于回文字符串的问题,LeetCode上关于回文串的题有十来道呢,也算一个比较重要的知识点。但是这道题确实不算一道难题,给了我们一个字符串,让我们找出可以组成的最长的回文串的长度,由于字符顺序可以打乱,所以问题就转化为了求偶数个字符的个数,我们了解回文串的都知道,回文串主要有两种形式,一个是左右完全对称的,比如noon, 还有一种是以中间字符为中心,左右对称,比如bob,level等,那么我们统计出来所有偶数个字符的出现总和,然后如果有奇数个字符的话,我们取取出其最大偶数,然后最后结果加1即可,参见代码如下:

解法一:

class Solution {
public:
int longestPalindrome(string s) {
int res = ;
bool mid = false;
unordered_map<char, int> m;
for (char c : s) ++m[c];
for (auto it = m.begin(); it != m.end(); ++it) {
res += it->second;
if (it->second % == ) {
res -= ;
mid = true;
}
}
return mid ? res + : res;
}
};

上面那种方法是通过哈希表来建立字符串和其出现次数的映射,这里我们可以换一种思路,来找出搜有奇数个的字符,我们采用的方法是使用一个set集合,如果遍历到的字符不在set中,那么就将其加入set,如果已经在set里了,就将其从set中删去,这样遍历完成后set中就是所有出现个数是奇数个的字符了,那么我们最后只要用s的长度减去0和set长度减一之间的较大值即可,为啥这样呢,我们想,如果没有出现个数是奇数个的字符,那么t的长度就是0,减1成了-1,那么s的长度只要减去0即可;如果有奇数个的字符,那么字符个数减1,就是不能组成回文串的字符,因为回文串最多允许一个不成对出现的字符,参见代码如下:

解法二:

class Solution {
public:
int longestPalindrome(string s) {
unordered_set<char> t;
for (char c : s) {
if (!t.count(c)) t.insert(c);
else t.erase(c);
}
return s.size() - max(, (int)t.size() - );
}
};

最后这种方法利用到了STL中的count函数,就是找字符串中某个字符出现的个数,那么我们和1相与,就可以知道该个数是奇数还是偶数了,返回的写法和上面那种方法相同,参见代码如下:

解法三:

class Solution {
public:
int longestPalindrome(string s) {
int odds = ;
for (char c = 'A'; c <= 'z'; ++c) {
odds += count(s.begin(), s.end(), c) & ;
}
return s.size() - max(, odds - );
}
};

类似题目:

Palindrome Pairs

Palindrome Permutation II

Palindrome Permutation

Palindrome Linked List

Shortest Palindrome

Palindrome Partitioning II

Palindrome Partitioning

Valid Palindrome

Palindrome Number

Longest Palindromic Substring

参考资料:

https://discuss.leetcode.com/topic/61338/what-are-the-odds-python-c

https://discuss.leetcode.com/topic/61574/very-easy-to-understand-java-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

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

  1. Longest Palindrome 最长回文串问题

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

  2. 409 Longest Palindrome 最长回文串

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

  3. [LeetCode] 409. Longest Palindrome 最长回文

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

  4. 从0打卡leetcode之day 6--最长回文串

    题目描述 给定一个字符串 s,找到 s中最长的回文子串.你可以假设 s 的最大长度为1000. 示例1 输入: "babad" 输出: "bab" 注意: &q ...

  5. leetcode.字符串.409最长回文串-Java

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

  6. LeetCode409Longest Palindrome最长回文串

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

  7. [LeetCode] Largest Palindrome Product 最大回文串乘积

    Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...

  8. POJ----(3974 )Palindrome [最长回文串]

    Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 5121   Accepted: 1834 Description Andy ...

  9. leetcode 每日签到 409. 最长回文串

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

随机推荐

  1. CSS教程:div垂直居中的N种方法以及多行文本垂直居中的方法

    在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...

  2. 关系数据库SQL之可编程性触发器

    前言 前面关系数据库SQL之可编程性函数(用户自定义函数)一文提到关系型数据库提供了可编程性的函数.存储过程.事务.触发器及游标,前文已介绍了函数.存储过程.事务,本文来介绍一下触发器的使用.(还是以 ...

  3. 控制反转、依赖注入、Unity容器

    控制反转原则 依赖注入 Install-Package Unity:https://www.nuget.org/packages/Unity/ Github:https://github.com/un ...

  4. 学习EF之贪懒加载和延迟加载(2)

    通过昨天对EF贪婪加载和延迟加载的学习,不难发现,延迟加载还是很好用的,但是问题也就来了,有的时候我们只需要加载一个实体,不需要和他相关的外部实体,这时候我们来看看EF延迟加载时怎么作用的吧 打开pr ...

  5. AutoResetEvent ManualResetEvent WaitOne使用注意事项

    公司还用这些老家伙没办法,用了几次这俩.每次用都要重新翻一下A片. 好好的A片楞是翻译成了禅经.把这东西弄成个玄学.微软也是吃枣药丸.参考了@风中灵药的blog.写的牛逼. 还有一些公司用到的风中灵药 ...

  6. python 数据类型---列表使用之三

    1. 判断列表中是否存在一个元素: "in" 的使用 list = ['Frank', 99, 'is',78, 7,3,4,'smart'] print(99 in list) ...

  7. php调用web service接口(.net开发的接口)

    实例代码1: try { $this->soapClientObj = new SoapClient(self::URL . '?wsdl', array('connection_timeout ...

  8. 深入理解 RESTful Api 架构

    转自https://mengkang.net/620.html 一些常见的误解 不要以为 RESTful Api  就是设计得像便于 SEO 的伪静态,例如一个 Api 的 URL 类似于 http: ...

  9. querySelectorAll 方法相比 getElementsBy 系列方法区别

    最近有人问到querySelectorAll 方法相比 getElementsBy 系列方法区别,一时没想起来说些什么,今天查下文档,总结一下它们的区别,以便自己理解. 1. W3C 标准queryS ...

  10. 解决mysql卸载后无法从新安装,卡在最后一步的问题

    mysql服务出现问题往往是最麻烦的,往往需要重装,而重装很多人卸不干净残留文件,更加装不上.在下就遇到这个问题.重装mysql到最后一步时卡在了最后一步的第二条上 解决办法就是卸载后删注册表+删数据 ...