409. Longest Palindrome
DifficultyEasy
Description
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
Letters are case sensitive, for example, "Aa" is not considered a palindrome.
Example 1:
Input: s = "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
Example 2:
Input: s = "a" Output: 1 Explanation: The longest palindrome that can be built is "a", whose length is 1.
Constraints:
1 <= s.length <= 2000sconsists of lowercase and/or uppercase English letters only.
Solutions
Solution 1: Counting
Thinking
A palindrome allows at most one odd-count character; the rest must pair. We do not need to try permutations, only how many pairs each character can give.
After counting, add \(v//2\times 2\) for every frequency \(v\). If the total is still shorter than \(|s|\), one leftover character can sit in the center.
Taking pairs first, then the optional center, yields a maximum feasible length.
A valid palindrome string can have at most one character that appears an odd number of times, and the rest of the characters appear an even number of times.
Therefore, we can first traverse the string \(s\), count the number of occurrences of each character, and record it in an array or hash table \(cnt\).
Then, we traverse \(cnt\), for each count \(v\), we divide \(v\) by 2, take the integer part, multiply by 2, and add it to the answer \(ans\).
Finally, if the answer is less than the length of the string \(s\), we increment the answer by one and return \(ans\).
The time complexity is \(O(n + |\Sigma|)\), and the space complexity is \(O(|\Sigma|)\). Where \(n\) is the length of the string \(s\), and \(|\Sigma|\) is the size of the character set. In this problem, \(|\Sigma| = 128\).
1 2 3 4 5 6 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Solution 2: Bit Manipulation + Counting
Thinking
Solution 1 counts, then sums. XORing a parity flag while scanning and tracking the number of odd characters \(\textit{cnt}\) gives \(n-\textit{cnt}+1\) (keep one center) or \(n\).
The second pass over the frequency table disappears; space is still the alphabet size.
We can use an array or hash table \(odd\) to record whether each character in string \(s\) appears an odd number of times, and an integer variable \(cnt\) to record the number of characters that appear an odd number of times.
We iterate through the string \(s\). For each character \(c\), we flip \(odd[c]\), i.e., \(0 \rightarrow 1\), \(1 \rightarrow 0\). If \(odd[c]\) changes from \(0\) to \(1\), then we increment \(cnt\) by one; if \(odd[c]\) changes from \(1\) to \(0\), then we decrement \(cnt\) by one.
Finally, if \(cnt\) is greater than \(0\), the answer is \(n - cnt + 1\), otherwise, the answer is \(n\).
The time complexity is \(O(n)\), and the space complexity is \(O(|\Sigma|)\). Where \(n\) is the length of the string \(s\), and \(|\Sigma|\) is the size of the character set. In this problem, \(|\Sigma| = 128\).
1 2 3 4 5 6 7 8 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 | |