当前位置:网站首页>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
边栏推荐
- torch. histc
- Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
- Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
- [TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
- 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
- 165. 比较版本号
- 4、【WebGIS实战】软件操作篇——数据导入及处理
- 6. zigzag transformation
- 【TA-霜狼_may-《百人计划》】2.1 色彩空间
- 后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
猜你喜欢

Unexpected token o in JSON at position 1 ,JSON解析问题
![[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation](/img/b3/887d3fb64acbf3702814d32e2e6414.png)
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation

Test function in pychram

pytorch训练深度学习网络设置cuda指定的GPU可见

Appium automation test foundation -- supplement: c/s architecture and b/s architecture description

RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs

BluePrism注册下载并安装-RPA第一章

Explain spark operation mode in detail (local+standalone+yarn)

数据库中COMMENT关键字的使用

Blueprism registration, download and install -rpa Chapter 1
随机推荐
6. Z 字形变换
Appium自动化测试基础 — APPium基本原理
数据库中COMMENT关键字的使用
后台系统右边内容如何出现滚动条和解决双滚动条的问题
【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
Use of comment keyword in database
214. 最短回文串
10. 正则表达式匹配
torch.histc
5、【WebGIS实战】软件操作篇——服务发布及权限管理
10、Scanner.next() 无法读取空格/indexOf -1
Sort linked list (merge sort)
Feature pyramid networks for object detection
165. 比较版本号
Review column - message queue
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
BluePrism注册下载并安装-RPA第一章
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
Cygwin的下载和安装配置
完全背包问题