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. Hibernate-注解-实体类

        @Entity //继承策略.另一个类继承本类,那么本类里的属性应用到另一个类中 @Inheritance(strategy = InheritanceType.JOINED ) @Table ...

  2. dede去掉当前位置position后面的箭头

    理论是,dede的当前位置标签{dedefield name='position'},结构是 "首页 > 主栏目 > 子栏目 > ",箭头符号字段数据都是在后台设 ...

  3. spring第一课,beans配置(上)

    1.通过property配置bean <!-- 配置一个 bean --> <bean id="helloWorld" class="com.atgui ...

  4. jquery 模糊查询下拉框

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD&g ...

  5. 简单工厂VS工厂方法

    前言: GOF经典的23种设计模式在IT界现已被广为流传.由于比较长时间没有用了,个人对于不同模式与模式之间的区别也渐渐模糊,故开始重温设计模式的思想.也希望更给对设计模式感兴趣的朋友些许的启发. - ...

  6. the comment lines of the blast tabular format

    进行Blast比对,用参数-m 6 可以以列表的方式输出结果,结果中从左到右每一列的意义分别是: [00] Query id [01] Subject id [02] % identity [03] ...

  7. 【BZOJ-2342】双倍回文 Manacher + 并查集

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1799  Solved: 671[Submit][Statu ...

  8. Matlab读入含有特殊分隔符的文件(textread)

    笔者在此基础上进行运行,修改得到以下内容,希望大家给与补充: textread 基本语法是: [A,B,C,…] = textread(filename,format) [A,B,C,…] = tex ...

  9. JSTL——formatNumber标签

    使用场合: <fmt:formatNumber>标签用于格式化数字,百分比,货币 属性: 语法 如果使用pattern属性.这个属性可以让您在对数字编码时包含指定的字符.接下来的表格中列出 ...

  10. InternalsVisibleToAttribute——把internal成员暴露给指定的友元程序集

    友元程序集简介 我们知道一个类中被定义为internal的成员(包括类型.方法.属性.变量.事件)是只能在同一个程序集中被访问到的(当然了,我这里说的是正常的方式,不包括通过反射来访问).这个规则在. ...