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. [译]git config

    git config git config命令用来设置git的一些配置(包括全局配置和针对单个仓储的配置).git config命令能定义一个仓储的用户信息和用户偏好. 用法 git config u ...

  2. 清北暑假模拟day1 艳阳天

    /* 注意P有可能不是质数,不要用欧拉函数那一套,正解可以倍增,就是等比数列和的性质,注意n是否为奇数 */ #include <cstdio> #include <algorith ...

  3. 一道Twitter面试题

    在微博上看到的这个问题,忍住没看答案自己解决了.建议没看过的同学也自己先尝试下. “看下面这个图片” 在这个图片里我们有不同高度的墙.这个图片由一个整数数组所代表,数组中每个数是墙的高度.上边的图可以 ...

  4. Ubuntu 12 修改环境变量

    Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环境变量.系统环境变量对所有系统用户都有效,用户环境变量仅仅对当前的用户有效. 修改用户环境变量 用户环境变量通常被存储在下面的文件中: ...

  5. php数据结构与算法

    php面试题之二--数据结构和算法(高级部分) 二.数据结构和算法 1.使对象可以像数组一样进行foreach循环,要求属性必须是私有.(Iterator模式的PHP5实现,写一类实现Iterator ...

  6. centos7 静态ip设置

    TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV ...

  7. Effective Java 学习笔记之所有对象都通用的方法

    一.覆盖equals时请遵守通用约定 1.满足下列任何一个条件时,不需要覆盖equals方法 a.类的每个实例本质上都是唯一的.此时就是Object中equals方法所表达的含义. b.不关心类是否提 ...

  8. Eclipse/MyEclipse 安装国际化资源文件编辑插件(i18n tools)

    一.JInto 官网:http://www.guh-software.de/index_en.html http://www.guh-software.de/jinto_en.html 下载地址:ht ...

  9. VirtualBox centos 6.5 minimal 开启网络

    默认情况下载的centos 6.5 minimal是不开启网卡功能的,按照下面的步骤开启网卡. vi /etc/sysconfig/network-script/ifcfg-eth0 将其中的 ONB ...

  10. JavaScript中var关键字的使用详解

    作用 声明作用:如声明个变量. 语法 ? 1 var c = 1; 省略var 在javascript中,若省略var关键字而直接赋值,那么这个变量为全局变量,哪怕是在function里定义的. ? ...