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.

题目的大体意思:

给定一个字符串包含大小写,用字符串中的字符组成一个回文串求符合要求的回文串的最大长度。

利用HashSet中去重复的特性,出现偶数重复的的话,就从set中remove出去,出现一次或者奇数次数的话,就会添加到HashSet中,根据String中总长度和set的长度判断回文的长度。

 public int longestPalindrome(String s) {
if(s==null|| s.length()==0)
return 0;
if(s.length()==1)
return 1;
HashSet<Character> set=new HashSet<Character>(); for(int i=0;i<s.length();i++){
if(set.contains(s.charAt(i))) {
//如果重复就
set.remove(s.charAt(i)); } else {
set.add(s.charAt(i));
} } if(set.size()>0)
return s.length()-set.size()+1;
else
return s.length()-set.size();
}

409. Longest Palindrome的更多相关文章

  1. 24. leetcode 409. Longest Palindrome

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

  2. 【leetcode】409. Longest Palindrome

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

  3. LeetCode 409 Longest Palindrome

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

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

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

  5. 409. Longest Palindrome 最长对称串

    [抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...

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

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

  7. LeetCode——409. Longest Palindrome

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

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

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

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

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

随机推荐

  1. C#-面向对象的三大特性——封装(封装成员变量、封装成员方法)

    封装: 封装就是将数据或函数等集合在一个个的单元中(我们称之为类).被封装的对象通常被称为抽象数据类型. 封装的意义在于保护或者防止代码(数据)被我们无意中破坏. 封装既可以封装成员变量,又可以封装成 ...

  2. 使用fiddler查看https请求

    首先点击菜单栏Tools>>>Fiddler Options>>>HTTPS 把Decrypt HTTPS Traffic 复选框勾选上 勾上之后,会弹窗提示你. ...

  3. selenium3各种报错解决办法

    解决办法全在这个链接里 http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3

  4. c# 读取嵌入式文件

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflect ...

  5. 【BZOJ-1097】旅游景点atr SPFA + 状压DP

    1097: [POI2007]旅游景点atr Time Limit: 30 Sec  Memory Limit: 357 MBSubmit: 1531  Solved: 352[Submit][Sta ...

  6. 【codevs1043】 方格取数

    http://codevs.cn/problem/1043/ (题目链接) 题意 N*N的方格,每个格子中有一个数,寻找从(1,1)走到(N,N)的两条路径,使得取到的数的和最大. Solution ...

  7. Linux内核邮件列表发送和回复格式研究

    1.使用的内容格式为[纯文本],这个在国内的客户端已经没有了,大公司只有微软的outlook. 2.回复引用时,使用符号[>]作为标记,且回复的内容不能在最顶部,应该在最下面.参考:http:/ ...

  8. 文件内容统计——Linux wc命令

    有了该命令,就可以得到当前目录下所有符合条件的文件总数,如下: find -type f | wc -l 这个命令的功能也很好记,因为它功能很有限: wc -c filename:显示一个文件的字节数 ...

  9. HDU 3038 How Many Answers Are Wrong(带权并查集)

    传送门 Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, ...

  10. JSP重定向小例子(不讲原理)

    编写一个JSP页面lucknum.jsp,产生0-9之间的随机数作为用户幸运数字,将其保存到会话中,并重定向到另一个页面showLuckNum.jsp中,在该页面中将用户的幸运数字显示出来 luckn ...