Leetcode 409. Longest Palindrome (Easy) (cpp)

来源:互联网 发布:淘宝纸箱机器 编辑:程序博客网 时间:2024/06/04 19:07

Leetcode 409. Longest Palindrome (Easy) (cpp)

Tag: Hash Table

Difficulty: Easy


/*409. Longest Palindrome (Easy)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:7Explanation:One longest palindrome that can be built is "dccaccd", whose length is 7.*/class Solution {public:    int longestPalindrome(string s) {        int table[58] = {0};        int res = 0;        bool flag = false;        for (char cha : s) {            table[cha - 'A']++;        }        for (auto i : table) {            res += i / 2 * 2;            if (i % 2 == 1) {                flag = true;            }        }        return flag == true ? res + 1 : res;    }};


0 0
原创粉丝点击