409. Longest Palindrome
409. Longest Palindrome
初雪之音 发表于8个月前
409. Longest Palindrome
  • 发表于 8个月前
  • 阅读 7
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

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:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

Code:

public class Solution {
    public int longestPalindrome(String s) {
		int result = 0;

		if (s != null && s.length() > 0) {
			int[] hash = new int[100];
			for (char vs : s.toCharArray()) {
				if (hash[vs - 'A'] == 1) {
					result += 2;
					hash[vs - 'A']--;
				} else {
					hash[vs - 'A']++;
				}
			}
			for (int vh : hash) {
				if (vh != 0) {
					result++;
					break;
				}
			}
		}

		return result;
	}
}

 

标签: leetcode
共有 人打赏支持
粉丝 37
博文 229
码字总数 129645
×
初雪之音
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: