原题链接在这里:https://leetcode.com/problems/longest-palindrome/

题目:

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.

题解:

Time Complexity: O(s.length()). Space: O(s.length()).

AC Java:

 public class Solution {
public int longestPalindrome(String s) {
if(s == null || s.length() == 0){
return 0;
} int res = 0;
HashSet<Character> hs = new HashSet<Character>();
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(hs.contains(c)){
hs.remove(c);
res++;
}else{
hs.add(c);
}
}
return hs.isEmpty() ? res*2 : res*2+1;
}
}

类似Palindrome Permutation.

LeetCode Longest Palindrome的更多相关文章

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

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

  2. [LeetCode] Longest Palindrome Substring 具体分析

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. LeetCode 409. Longest Palindrome (最长回文)

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

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

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

  5. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

  6. LeetCode 409 Longest Palindrome

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

  7. 24. leetcode 409. Longest Palindrome

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

  8. 【leetcode】409. Longest Palindrome

    problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...

  9. [LeetCode&Python] Problem 409. Longest Palindrome

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

随机推荐

  1. Java线程之CompletionService

    转自:http://blog.csdn.net/andycpp/article/details/8902699 当使用ExecutorService启动了多个Callable后,每个Callable会 ...

  2. DELPHI的split函数的各种实现方法(转)

    一.单字符 function split(s,s1:string):TStringList;beginResult:=TStringList.Create;while Pos(s1,s)>0 d ...

  3. Odoo Two ways to pop warning infomation

    1. raise ValueError(_('title'),_('message')) 2.raise except_orm(_('title'),_('message'))

  4. hdu A strange lift

    有起点和终点,有方向,有最少次数,所以这道题很明显是一道bfs的题目,这题要利用vist数组来标记已走过的楼层,因为这题里面已走过的楼层是不可能在走第二遍的. 第二次走和第一次走的选择没有任何的区别. ...

  5. Geronimo应用服务器和MySQL数据库服务器

    就是Web 2.0的全部,尽管该术语出现才几乎一年的时间,但现在好像只有烹饪杂志还没有加入到讨论Web 2.0未来出路的行列中.自从出现了里程碑式的文章"What Is Web 2.0:De ...

  6. 如何使用CREATE INDEX语句对表增加索引?

    创建和删除索引索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER TABLE来给表增加索引.删除索引可以利用ALTER TABLE或DROP INDE ...

  7. 8. Add the dashboard

    Controller Node: 1. sudo apt-get install apache2 memcached libapache2-mod-wsgi openstack-dashboard   ...

  8. hlg 2130 状压dp

    基本的状压dp 需要注意的是两点之间直线最短 所以不需要进行floyd 由于把dp的memset放在了初始化0的后面de了好久的bug.. #include<stdio.h> #inclu ...

  9. 有两个地方,用到了javabean对象和属性字符串值之间的转换

    1.有两个地方,用到了javabean对象和属性字符串值之间的转换 2.一个是接入层spring mvc,将json字符串参数转换为javaBean.通过@RequestBody javaBean方式 ...

  10. Ubuntu 卸载 VMware

    sudo vmware-installer --uninstall-product vmware-workstationsudo vmware-installer --uninstall-produc ...