Longest Palindrome

来源:互联网 发布:淘宝男士内裤哪个好 编辑:程序博客网 时间:2024/05/01 08:19

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.


int longestPalindrome(char* s) {    long long c[256] = {0};    int res = 0;    int hasodd = 0;    if (s == NULL) return 0;    while(*s != '\0') {        c[*s++]++;    }        for(int i = 0; i < 256; i++) {        res += c[i];        if(c[i]%2 == 1) {            hasodd = 1;            res -= 1;        }    }    return hasodd ? res+1:res;}


1 0
原创粉丝点击