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.

题目标签:Hash Table

  题目给了我们一个string s,让我们找到在这个s 里 最长可能性的回文的长度。

  利用HashMap 先把 s 里的所有char 和它出现次数 做记录,然后遍历map 的keySet,把所有char 长度是偶数的加起来,在把所有char 长度是奇数的 - 1 加起来,最后要判断一下,如果有出现过奇数长度的char,那么在最后答案上再 + 1。

Java Solution:

Runtime beats 58.91%

完成日期:06/06/2017

关键词:HashMap

关键点:对于奇数长度的char,我们也需要把它的 长度 - 1 累加

 class Solution
{
public int longestPalindrome(String s)
{
HashMap<Character, Integer> map = new HashMap<>();
int len = 0;
boolean oddChar = false; for(char c: s.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1); for(char c: map.keySet())
{
int temp_len = map.get(c); if(temp_len % 2 == 0) // even len
len += temp_len;
else // odd len
{
if(!oddChar)
oddChar = true; len += temp_len - 1;
}
} return oddChar ? len + 1 : len;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 409. Longest Palindrome (最长回文)的更多相关文章

  1. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  2. 409 Longest Palindrome 最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...

  3. leetcode 5 Longest Palindromic Substring--最长回文字符串

    问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. Longest Palindrome 最长回文串问题

    1.题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...

  5. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  6. 24. leetcode 409. Longest Palindrome

    409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...

  7. LeetCode 409 Longest Palindrome

    Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...

  8. LeetCode之“字符串”:最长回文子串

    题目要求: 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串.例如,给出字符串 "abcdzdcab",它的最长回文子串为 & ...

  9. LeetCode——409. Longest Palindrome

    题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...

  10. 转载:LeetCode:5Longest Palindromic Substring 最长回文子串

    本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...

随机推荐

  1. Selenium学习第二天,了解Selenium工作模式与学习Selenium需要具备的知识与工具。

    Selenium学习网站: 1.http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2014/0408/207237.html——好像是对API的 ...

  2. android cmd adb命令安装和删除apk应用

    copy自http://blog.csdn.net/xpsharp/article/details/7289910 1. 安装Android应用程序 1) 启动Android模拟器 2) adb in ...

  3. Android开发笔记(2)——ViewGroup

    笔记链接:http://www.cnblogs.com/igoslly/p/6794344.html 一.ViewGroup 1.ViewGroup的意义——整合Layout多个不同View,并对其进 ...

  4. 个人软件过程(psp)需求分析

    个人软件过程(psp)需求分析 1.  引言 1.1  背景 开发项目进度计划不准确,延期经常出现,甚至无法给出一个比较准确的延迟时间,给市场推广带来很大麻烦. 2.  任务概述 2.1 目标 PSP ...

  5. 解决[disabled]="true"与formControlName冲突

    import { FormBuilder } from '@angular/forms'; form; constructor(private fb: FormBuilder) { this.form ...

  6. js实现字符串反转

    方案1: var str = "abcdef"; console.log( str.split("").reverse().join("") ...

  7. 让浏览器不再显示 https 页面中的 http 请求警报<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests" ...

  8. 还没更换RubyGems镜像?

    相信用过Ruby的人都知道 gem install 命令,但是在国内该命令安装的速度甚是不稳定(你懂的),导致尝试数次便是等待数时,记得之前在安装redmine的时候便是如此,之前不懂什么意思,还以为 ...

  9. MYEclipse Available Memory is low 警告 解决方法

    1,  设置Eclipse内存使用情况 修改eclipse根目录下的eclipse.ini文件 -vmargs  //虚拟机设置 -Xms40m -Xmx256m -XX:PermSize=128M ...

  10. [Algorithm] 6. Merge Two Sorted Arrays

    Description Merge two given sorted integer array A and B into a new sorted integer array. Example A= ...