原题链接在这里: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. elasticsearch1.0 升级2.2的数据备份和恢复

    近期由于elasticsearch的版本升级,需要研究下elasticsearch的快照(snapshot)和恢复(restore)功能.   先说下背景,目前环境采用的是elasticsearch1 ...

  2. C# params object[] args 可以传多个参数,可以不限制类型(转)

    C# params object[] args 可以传多个参数,可以不限制类型 using System;using System.Collections.Generic;using System.T ...

  3. 五、Pillar数据管理中心

    Pillar是数据管理中心. Pillar在saltstack中主要作用是存储和定义一些配置管理中需要的信息(比如:软件版本,用户名,密码等) 修改pillar相关配置文件: [root@super6 ...

  4. 一些开发遇到的"小问题",你能答对多少?

    我会把问题先写在前面,答案用白色字体写在后面.所以用鼠标选择文本就可以看到答案啦. 调用await后因为切换了线程环境(这种说法可能不严谨,但我只能想到这种说法),httpcontext会为null. ...

  5. java 中的instanceof的用法

    instanceof 运算符是Java.php的一个二元操作符(运算符),和==.>.<是同一类东西.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是判断其左边对象是否为其右 ...

  6. Servlet编程-步步为营

    [环境]eclipse j2ee;Tomcat 7.0; [模型1] package com.zhiqi; import ...; public class TestServlet extends H ...

  7. mongodb 3.0 版本分片部署步骤

    # linux 网络优化 1. 文件中/etc/sysctl.conf, 加入 net.core.somaxconn = 2048 fs.file-max = 2000000 fs.nr_open = ...

  8. Apache Spark源码走读之20 -- ShuffleMapTask计算结果的保存与读取

    欢迎转载,转载请注明出处,徽沪一郎. 概要 ShuffleMapTask的计算结果保存在哪,随后Stage中的task又是如何知道从哪里去读取的呢,这个过程一直让我困惑不已. 用比较通俗一点的说法来解 ...

  9. RDBMS vs. NoSQL 合作还是竞争

    欢迎转载,转载请注明出处,徽沪一郎. 由于近期手头的工作和数据库的选型相关,纠结于是否使用一款NoSQL数据库来替换已有的MySQL数据库.在这个过程中随着学习研究的深入,对于二者的异同有了一些初步的 ...

  10. Redis 笔记与总结3 list 类型

    redis 版本 [root@localhost ~]# redis-server --version Redis server v= sha=: malloc=jemalloc- bits= bui ...