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:

  1. Input:
  2. "abccccdd"
  3.  
  4. Output:
  5. 7
  6.  
  7. Explanation:
  8. One longest palindrome that can be built is "dccaccd", whose length is 7.

  1. from collections import Counter
  2. class Solution(object):
  3. def longestPalindrome(self, s):
  4. """
  5. :type s: str
  6. :rtype: int
  7. """
  8. c=Counter(s)
  9. if len(c)==1:
  10. return len(s)
  11. ans=0
  12. flag=False
  13. for i in c:
  14. if not flag and c[i]%2==1:
  15. flag=True
  16. ans+=c[i]//2*2
  17. if flag:
  18. ans+=1
  19. return ans

  

[LeetCode&Python] Problem 409. Longest Palindrome的更多相关文章

  1. 【leetcode❤python】409. Longest Palindrome

    #-*- coding: UTF-8 -*- from collections import Counterclass Solution(object):    def longestPalindro ...

  2. [LeetCode&Python] Problem 594. Longest Harmonious Subsequence

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  3. [LeetCode&Python] Problem 720. Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  4. [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...

  5. 【leetcode】409. Longest Palindrome

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

  6. 24. leetcode 409. Longest Palindrome

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

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

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

  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

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

随机推荐

  1. Physical Limits of ASM

    Oracle version 7, only 1,022 datafiles per database could be used.  Oracle version 11g, support 65,5 ...

  2. 思科恶意加密TLS流检测论文记录——由于样本不均衡,其实做得并不好,神马99.9的准确率都是浮云啊,之所以思科使用DNS和http一个重要假设是DGA和HTTP C&C(正常http会有图片等)。一开始思科使用的逻辑回归,后面17年文章是随机森林。

    论文记录:Identifying Encrypted Malware Traffic with Contextual Flow Data from:https://songcoming.github. ...

  3. 阿里云免费申请https证书

    申请地址   https://common-buy.aliyun.com/?spm=a2c4e.11153940.blogcont65199.22.30f968210RsUSx&commodi ...

  4. mongdb使用

    下载mongodb数据库  https://www.mongodb.com/ 根据自己的电脑系统下载相应的版本 安装并且打开你下载的数据库 打开数据库bin文件夹:          cd soft/ ...

  5. os 模块 和 os模块下的path模块

    import os # os 主要用于与操作系统进行交互 #获取当前的工作目录 print(os.getcwd()) #切换工作目录 os .chdir("D:\上海python全栈4期\d ...

  6. Kafka.net使用编程入门(三)

    这个世界既不是有钱人的世界,也不是有权人的世界,它是有心人的世界. 一些有用的命令 1.列出主题:kafka-topics.bat --list --zookeeper localhost:2181 ...

  7. unity中实现三个Logo图片进行若隐若现的切换并有延时切换图片的效果

    public GameObject canvas; private Transform logoParent; private Transform Logo_logo; //logo一 private ...

  8. TOleControl(WebBrowser1).Visible := False 这样就可以隐藏浏览器控件

    TOleControl(WebBrowser1).Visible := False 这样就可以隐藏浏览器控件了. ------------------------------------------- ...

  9. caffe的一些概念理解

    有一天,师姐问我,epoch和iteration有什么区别?我一时语塞,竟然遍寻百度而不得,最后在stackoverflow上找到一个我认为比较靠谱的答案,虽然它不是最高票,但是是最好理解的,深得我心 ...

  10. 将远程mysql服务器数据导出 csv 并发送到我的本机

    1.在SQL上执行查询并导出操作 '; 2.发送到本机 SQL>system sz idIsNull2.csv;