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. 【AngularJS】—— 11 指令的交互

    前面基本了解了指令的相关内容: 1 如何自定义指令 2 指令的复用 本篇看一下指令之间如何交互.学习内容来自<慕课网 指令3> 背景介绍 这例子是视频中的例子,有一个动感超人,有三种能力, ...

  2. poj3335 半交平面,多边形内核

    Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5300   Accepted: 21 ...

  3. Handler Should be static or leaks Occur?

    解决办法: public class SampleActivity extends Activity { /** * Instances of static inner classes do not ...

  4. post 之checkbox 未被选中解决方法

    第一种方法: http://cnn237111.blog.51cto.com/2359144/1293812 第二种方法(推荐): http://blog.csdn.net/xyanghomepage ...

  5. 信号屏蔽的切换的理解sigsuspend

    #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h&g ...

  6. javascript中对象学习

    第一篇文章: javascript中this关键字的详细解析:   http://blog.csdn.net/wyj880220/article/details/7305952 Javascript ...

  7. [Leetcode19] Remove Nth Node From End of List

    视频讲解  http://v.youku.com/v_show/id_XMTY1MTMzNjAyNA==.html (1)定义两个指针 ListNode fast = head; ListNode s ...

  8. C++ STL,list vector区别

    顺序性容器: 向量 vector :   是一个线性顺序结构.相当于数组,但其大小可以不预先指定,并且自动扩展.它可以像数组一样被操作,由于它的特性我们完全可以将vector 看作动态数组.在创建一个 ...

  9. fastx_toolkit去除测序数据中的接头和低质量的reads

    高通量测序数据下机后得到了fastq的raw_data,通常测序公司在将数据返还给客户之前会做"clean"处理,即得到clean_data.然而,这些clean_data是否真的 ...

  10. java文件压缩和解压

    功能实现. package com.test; import java.io.File; import java.io.BufferedOutputStream; import java.io.Buf ...