当前位置:网站首页>409. longest palindrome
409. longest palindrome
2022-07-01 03:44:00 【Sun_ Sky_ Sea】
409. Longest palindrome
Original title link :https://leetcode.cn/problems/longest-palindrome/
Given a string of uppercase and lowercase letters s , return Constructed from these letters The longest palindrome string .
In the process of construction , Please note that Case sensitive . such as “Aa” Can't be treated as a palindrome string .
Example 1:
Input :s = “abccccdd”
Output :7
explain :
The longest palindrome string we can construct is "dccaccd", Its length is 7.
Example 2:
Input :s = “a”
Input :1
Example 3:
Input :s = “bb”
Input : 2
Tips :
1 <= s.length <= 2000
s Only lowercase and / Or capital letters
Their thinking :
Count the number of characters , Even numbers of characters can be placed on either side , The odd number of characters can only be used once , Among them, even characters are placed on both sides , One remaining character , Put it in the middle as the dividing line .
Code implementation :
class Solution:
def longestPalindrome(self, s: str) -> int:
# Statistics string s The number of each character of
import collections
count = collections.Counter(s)
ans = 0
for value in count.values():
# Take an even number of characters each time and put them on both sides of the palindrome string
# ans The number of characters used in the record , Length of palindrome string
ans += value // 2 * 2
# If value If it's an odd number , Then use this odd string once
# because value The even number of characters in can be placed on both sides of the palindrome string
# The rest can be placed in the middle of the palindrome string as the dividing line
# Such a number is an odd number of characters can only use one , Otherwise, it will not form a palindrome string
if ans % 2 == 0 and value % 2 == 1:
ans += 1
return ans
边栏推荐
- [TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
- Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
- 在 C 中声明函数之前调用函数会发生什么?
- Feature pyramid networks for object detection
- 访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
- 使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
- 数据库中COMMENT关键字的使用
- Gorilla/mux framework (RK boot): RPC error code design
- Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
- Ultimate dolls 2.0 | encapsulation of cloud native delivery
猜你喜欢

完全背包问题

Home online shopping project

【TA-霜狼_may-《百人计划》】1.3纹理的秘密

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products

二叉树神级遍历:Morris遍历

How keil displays Chinese annotations (simple with pictures)

MFC窗口滚动条用法

Download and installation configuration of cygwin

Error: plug ins declaring extensions or extension points must set the singleton directive to true

不用加减乘除实现加法
随机推荐
214. 最短回文串
Edlines: a real time line segment detector with a false detection control
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
[reach out to Party welfare] developer reload system sequence
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
Test function in pychram
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
【TA-霜狼_may-《百人计划》】1.3纹理的秘密
Valid brackets (force deduction 20)
完全背包问题
[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
208. 实现 Trie (前缀树)
Pytorch training deep learning network settings CUDA specified GPU visible
Use of comment keyword in database
187. 重复的DNA序列
复习专栏之---消息队列
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
Split(), split(), slice(), can't you tell?
Cygwin的下载和安装配置