Leetcode 409 Longest palindrome

Source: Internet
Author: User

Problem:

Given A string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be bu Ilt with those letters.

This is case sensitive, and for example are not "Aa" considered a palindrome.

Assume the length of given string would not exceed 1,010.

Example:

Input: "ABCCCCDD" Output:7explanation:one longest palindrome that can being built is "DCCACCD" and whose length is 7.

Summary:

Gives a string that may contain uppercase and lowercase letters, and the longest palindrome string length that can be composed of letters in a string. Uppercase and lowercase letters here are considered to be different.

Analysis:

1. Palindrome string contains two forms: ABA form and AAA form. In the given string, all occurrences of an even number of letters can be used in a palindrome string, if there is an odd number of letters, it will first include the largest even number of the palindrome string length (that is, odd number of times minus 1), and then put in the middle of the last letter. The following is a method for mapping letters and occurrences of a hash table.

1 classSolution {2  Public:3     intLongestpalindrome (strings) {4         intLen = S.size (), ch[ the] = {0}, res =0;5         BOOLOdd =false;6         7          for(inti =0; i < Len; i++) {8             if(S[i] >='a'&& S[i] <='Z') {9Ch[s[i]-'a']++;Ten             } One             Else { ACh[s[i]-'A'+ -]++; -             } -         } the          -          for(inti =0; I < the; i++) { -             if(Ch[i]%2==0) { -Res + =Ch[i]; +             } -             Else { +Odd =true; ARes + = Ch[i]-1; at             } -         } -          -         returnOdd? Res +1: res; -     } -};

2. As has been analyzed, the resulting palindrome string length of all occurrences of an even number of letters in the original string, as well as the number of odd times the maximum number of letters, and finally add a palindrome in the middle of a letter (with odd number of times the letter).

This result is equivalent to the original string, each occurrence of an odd number of letters minus one, and eventually add a palindrome in the middle of the letter. The following code counts the number of occurrences of each letter with Count, and the 1-& operation to determine whether it is odd.

1 classSolution {2  Public:3     intLongestpalindrome (strings) {4         intOdd =0, Len =s.size ();5          for(inti =0; I < -; i++) {6             Charc = i +'a';7Odd + = count (S.begin (), S.end (), C) &1;8         }9         Ten          for(inti =0; I < -; i++) { One             Charc = i +'A'; AOdd + = count (S.begin (), S.end (), C) &1; -         } -          the         returnOdd? Len-odd +1: Len; -     } -};

Leetcode 409 Longest palindrome

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.