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.

解释:构成回文字符串,最好的情况就是所有出现双数的字母都排上,中间再加一个单着的

方法一:哈希表
class Solution {
    public int longestPalindrome(String s) {
        int count=0;
        int tip=0;
        HashMap<Character,Integer> map=new HashMap<Character,Integer>();
        for(char c:s.toCharArray()){
           if(map.containsKey(c)) {
               int nums=map.get(c);
               map.put(c,nums+1);
           }else{
               map.put(c,1);
           }
        }

        for(char c:s.toCharArray()){
            if(map.containsKey(c)) {
               int nums=map.get(c);
               if(nums%2==1){
                   tip=1;
               }
                count+=nums/2;
                map.remove(c);
           }
        }
        return count*2+tip;
    }
}
方法二:数组这种方法思路很简单,但是空间和时间消耗都很大。
class Solution {
    public int longestPalindrome(String s) {
        int[] chr = new int[256];
        for(char c:s.toCharArray()){
            chr[c]++;
        }
        int p=0;
        for(int c:chr){
            p+=(c/2)*2;
        }
        if(p<s.length()){
            p++;
        }
        return p;
    }
}

原文地址:https://www.cnblogs.com/shaer/p/10847122.html

时间: 2024-09-30 09:44:31

409. Longest Palindrome(计算一组字符集合可以组成的回文字符串的最大长度)的相关文章

51Nod - 1092 回文字符串(添加删除字符LCS变形)

回文字符串 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba.方案1只需要添加2个字符,是所有方案中添加字符数量最少的. Input输入一个字符串Str,Str的长度 <= 1000.Output输出最少添加多少个字符可以使之变为回文字串.Sample Input abbc Sample Output 2 向字符串中间添加(

letecode [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 lengt

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

Java Longest Palindromic Substring(最长回文字符串)

假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic string.如aba,或者abba.本题是这种,给定输入一个字符串.要求输出一个子串,使得子串是最长的padromic string. 下边提供3种思路 1.两側比較法 以abba这样一个字符串为例来看,abba中,一共同拥有偶数个字.第1位=倒数第1位.第2位=倒数第2位......第N位=倒数第N位 以aba这样一个字符串为例来看,aba中.一共同拥有奇数个字符.排除掉正中间的那个字符后,第1位=倒数第1

Validate Palindrome 验证回文字符串

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "race a car" is not a palindrome. Note:Have you consider that the string might be empty? This is a good question to ask du

#计算回文字符串最大长度模板题

计算回文字符串最大长度模板题 如果一个字符串正着读和倒着读是一样的,则称它是回文的. 给定一个长度为N的字符串S,求他的最长回文子串的长度是多少. 输入格式 输入将包含最多30个测试用例,每个测试用例占一行,以最多1000000个小写字符的形式给出. 输入以一个以字符串"END"(不包括引号)开头的行表示输入终止. 输出格式 对于输入中的每个测试用例,输出测试用例编号和最大回文子串的长度(参考样例格式). 每个输出占一行. 输入样例: abcbabcbabcba abacacbaaaa

131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

131. Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["

5.13 添加最少字符使字符串整体都是回文字符串

[题目]: 给定一个字符串str,如果可以在str的任意位置添加字符,请返回在添加字符最少的情况下,让str整体都是回文字符串的一种结果 举例: str="ABA",str本身就是回文串,不需要添加字符,所以返回"ABA" str="AB",可以在'A'之前添加'B',使str整体都是回文串,故可以返回"BAB",也可以在'B'之后添加'A',使str整体都是回文串,故也可以返回"ABA",总之,只要添加的

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 l