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 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.

题目大意

  给定一个字符串,计算使用这些字符能够组成的回文串的最大长度。

理  解:

  当某个字符出现偶数次时,即可用于组成回文串,回文串中至多允许一个字符出现一次。

代 码 C++:

class Solution {
public:
    int longestPalindrome(string s) {
        set<char> m_set;
        set<char>::iterator itr;
        int count = 0;
        for(char ch:s){
            itr = m_set.find(ch);
            if(itr!=m_set.end()){
                count = count + 2;
                m_set.erase(itr);
            }else{
                m_set.insert(ch);
            }
        }
        if(!m_set.empty())
            count++;
        return count;
    }
};

运行结果:

  执行用时 :20 ms, 在所有 C++ 提交中击败了10.87%的用户

  内存消耗 :10.5 MB, 在所有 C++ 提交中击败了5.02%的用户

原文地址:https://www.cnblogs.com/lpomeloz/p/11066792.html

时间: 2024-08-01 15:23:59

letecode [409] - Longest Palindrome的相关文章

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

LeetCode 409 Longest Palindrome

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 l

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

[LeetCode&amp;Python] Problem 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

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

Uva 11151 - Longest Palindrome

A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-palindromic string, you can al

LeetCode Longest Palindrome

原题链接在这里:https://leetcode.com/problems/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&qu

[LeetCode] 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 leng

5.Longest Palindrome substring

/* * 5.Longest Palindrome substring * 2016-4-9 by Mingyang 自然而然的想到用dp来做 * 刚开始自己做的时候分的条件太细,两个index相等,相差小于3,还有其他 * 但这里这个if写的很好,一个代表了所有为true的情况 * 根本不用管为false的情况,因为自然而然为false * */ public String longestPalindrome(String s) { String res = " "; if (s =