[LeetCode] 409. Longest Palindrome 最长回文
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 最长回文的更多相关文章
- 409 Longest Palindrome 最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- Longest Palindrome 最长回文串问题
1.题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 24. leetcode 409. Longest Palindrome
409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...
- LeetCode 409 Longest Palindrome
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- LeetCode之“字符串”:最长回文子串
题目要求: 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串.例如,给出字符串 "abcdzdcab",它的最长回文子串为 & ...
- LeetCode——409. Longest Palindrome
题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...
- 转载:LeetCode:5Longest Palindromic Substring 最长回文子串
本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...
随机推荐
- 玩转Fiddler抓包工具
一.Fiddler简述 Fiddler是最强大最好用的Web调试工具之一, 它能记录所有客户端和服务器的http和https请求.允许你监视.设置断点.甚至修改输入输出数据.Fiddler包含了一个强 ...
- What is Code Quality?
Ref detail : https://realpython.com/python-code-quality/ What is Code Quality? Of course you want qu ...
- 最新NetMonitor代码
<Window x:Class="NetMonitor.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...
- C++报错:全局变量重定义或是多次定义
如何在C++中定义全局变量时避免重复定义呢? 只要在定义时尽量在.cpp文件中进行,而不要在.h 文件中定义,定义好了之后,可以在.h文件中利用extern关键字进行声明. 如果在.h文件中定义的话, ...
- 按值传递与按值引用详解(java版)
http://blog.csdn.net/zzp_403184692/article/details/8184751
- C/C++中double类型的比较
由于double浮点数的精度问题,所以在比较大小的时候,不能像int整数型那样,直接if(a==b),if(a<b),if(a>b) 要使用一个精度EPS: ; //一般这样子就够,但有时 ...
- S1_搭建分布式OpenStack集群_09 cinder 控制节点配置
一.创建数据库创建数据库以及用户:# mysql -uroot -p12345678MariaDB [(none)]> CREATE DATABASE cinder;MariaDB [(none ...
- C++编译器和连接器原理
本文转载自新浪永远即等待的博客 几个概念: 1.编译:编译器对源文件进行编译,就是把源文件中的文本形式存在的源代码翻译成机器语言形式的目标文件的过程,在这个过程中,编译器会进行一系列的语法检查.如果 ...
- 设置make为内部命令
在Windows中下载Dev-cpp,配置环境变量,在MinGW64\bin下的mingw32-make.exe改名为make.exe,即可在命令行中使用make命令.
- uni-app 模拟器
1.安装MuMu模拟器 Android模拟器端口: 7555 2.安装夜神模拟器 Android模拟器端口: 62001