LeetCode刷题实战409:最长回文串
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 here.
示例
"abccccdd"
输出:
7
解释:
我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
解题
class Solution {
public int longestPalindrome(String s) {
int[] count = new int[58];
for (int i=0;i<s.length();i++)
count[s.charAt(i)-'A']++;
int ans = 0;
for (int j:count)
ans+=(j/2)*2;
if (ans < s.length())
ans++;
return ans;
}
}
赞 (0)