Problem:

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.

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.

Summary:

给出一个可能包含大写、小写字母的字符串,求用字符串中的字母可组成的最长回文串长度。此处大写、小写字母视为有区别。

Analysis:

1. 回文串包含两种形式:aba形式及aaa形式。在给出的字符串中,所有出现次数为偶数的字母都可以用为回文串中;若有出现次数为奇数的字母,则先将其包含的最大偶数次加入回文串长度中(即奇数次数减1),再在最后加上放在最中间的一个字母。下面为Hash表映射字母和出现次数的方法。

 class Solution {
public:
int longestPalindrome(string s) {
int len = s.size(), ch[] = {}, res = ;
bool odd = false; for (int i = ; i < len; i++) {
if (s[i] >= 'a' && s[i] <= 'z') {
ch[s[i] - 'a']++;
}
else {
ch[s[i] - 'A' + ]++;
}
} for (int i = ; i < ; i++) {
if (ch[i] % == ) {
res += ch[i];
}
else {
odd = true;
res += ch[i] - ;
}
} return odd ? res + : res;
}
};

2. 如上已分析,得到的回文串长度为原字符串中所有出现偶数次的字母次数,以及出现奇数次的字母次数中的最大偶数,最终再加上回文串中间的一个字母(有奇数次字母时)。

这个结果相当于在原字符串中,将每一个出现奇数次的字母减掉一个,最终加上回文串中间的字母。下面的代码用count统计每个字母出现的次数,和1作&操作来判断是否为奇数。

 class Solution {
public:
int longestPalindrome(string s) {
int odd = , len = s.size();
for (int i = ; i < ; i++) {
char c = i + 'a';
odd += count(s.begin(), s.end(), c) & ;
} for (int i = ; i < ; i++) {
char c = i + 'A';
odd += count(s.begin(), s.end(), c) & ;
} return odd ? len - odd + : len;
}
};

LeetCode 409 Longest Palindrome的更多相关文章

  1. 24. leetcode 409. Longest Palindrome

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

  2. LeetCode 409. Longest Palindrome (最长回文)

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

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

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

  4. LeetCode——409. Longest Palindrome

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

  5. 【leetcode】409. Longest Palindrome

    problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...

  6. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

  7. [LeetCode&Python] Problem 409. Longest Palindrome

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

  8. 409. Longest Palindrome

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

  9. 409. Longest Palindrome 最长对称串

    [抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...

随机推荐

  1. sql拼音简写函数

    USE [HotelDB]GO /****** Object: UserDefinedFunction [dbo].[fn_GetPy] Script Date: 2016/1/4 13:29:13 ...

  2. AngularJS 国际化——Angular-translate

    对于一个用户群面向全球的的应用来说,不得不考虑国际化的问题.当然,即便是刚刚起步的小应用,如果有心搞大,也应该提前设计国际化的方案. 本篇讲述使用AngularJS构建的应用的简单国际化方案,准确的说 ...

  3. 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)

    Graph Reconstruction Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Let there ...

  4. Go - template 常用方法详解 及 注意事项

    Go template包下面有两个函数可以创建模板实例 func New(name string) *Template func ParseFiles(filenames ...string) (*T ...

  5. Nginx初学者指南

    Starting, Stopping, and Reloading Configuration To start nginx, run the executable file. Once nginx ...

  6. SpringDataJPA的几个使用记录

    public Page<XMGLFileTemplateDTO> findXMGLFileTemplateByConditions(XMGLFileTemplateDTO xmglFile ...

  7. 常见HTTP错误代码大全

    一些常见的状态码为: 200 - 服务器成功返回网页404 - 请求的网页不存在503 - 服务不可用详细分解: 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明100 ...

  8. [KOJ0574NOIP200406合并果子]

    [COJ0574NOIP200406合并果子] 试题描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆.    每一次合并,多多可以把两 ...

  9. OpenCV成长之路(6):数学形态学基本操作及其应用

    数学形态学实际上可以理解为一种滤波行为,所以很多地方称它为形态学滤波.有了个这概念,我们就能更好的理解它.我们滤波中用的滤波器(kernel)在这里被称为结构元素,结构元素往往是由一个特殊的形状构成, ...

  10. 浅析mongoEngine的document对象

    引言: from mongoengine import * connect('local')class Test(Document): name=StringField(max_length=32) ...