zoukankan      html  css  js  c++  java
  • 409. 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.
    his 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.
    

    给定一组字符串,求能够组合出最长的回文串,分为两种情况:

    • 字符串内每个字符都为偶数个,例如:'abccba',出现的字符种类为3,这种毫无疑问最大的长度为2 * 3
    • 某个字符出现了基数次,例如abccccdd,组成的 长度为2*3 + 1
      代码:
    public int longestPalindrome(String s) {
            if(s == null || s.length() == 0) return 0;
            HashSet<Character> hs = new HashSet<Character>();
            int count = 0;
            for(int i = 0; i < s.length(); i++)
            {
                if(hs.contains(s.charAt(i)))
                {
                    hs.remove(s.charAt(i));
                    count ++;
                }
                else
                    hs.add(s.charAt(i));
            }
            if(!hs.isEmpty()) return count * 2 + 1;
            return count * 2;
        }
    
  • 相关阅读:
    2. 开关电源.电感
    1. 开关电源.引子
    资源介绍
    3. EMC EMS EMI
    2. 基于MCU应用的EMC指南
    1. 内部管脚电路
    9.150 Predefined macros
    海康安防平台
    Redis常见配置
    利用python检测单词的相似度
  • 原文地址:https://www.cnblogs.com/wxshi/p/7770728.html
Copyright © 2011-2022 走看看