[409] C language brush LeetCode longest palindromic sequence (E)

Given a capital letter and lowercase letter string comprising, configured to find the longest by letters palindromic sequence.

In the construction process, please note case sensitive. Such as "Aa" can not be used as a palindrome string.

Note:
assuming that the length of the string is not more than 1,010.

Example 1:

Enter:
"abccccdd"

Output:
7

Explanation:
we can construct the longest string is palindrome "dccaccd", its length is 7.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/longest-palindrome
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Character, palindrome, of course, consider the hash string directly int arr [128] = {0}; back is simple.

int longestPalindrome(char * s){
    int len = strlen(s);
    int i;
    int retmax = 0;
    int arr[128] = {0};
    int flag = 0;
    
    for (i = 0; i < len; i++) {
        arr[s[i]]++;
    }
    
    for (i = 0; i < 128; i++) {
        if(arr[i] % 2) {
            retmax += arr[i] - 1;
            flag = 1;
        } else {
            retmax += arr[i];
        }
    }
    
    retmax = retmax + flag;
    
    return retmax;
}

 

Published 140 original articles · won praise 17 · views 110 000 +

Guess you like

Origin blog.csdn.net/jin615567975/article/details/104416506