当前位置:网站首页>409. 最长回文串
409. 最长回文串
2022-07-01 03:23:00 【Sun_Sky_Sea】
409. 最长回文串
原始题目链接:https://leetcode.cn/problems/longest-palindrome/
给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的回文串 。
在构造过程中,请注意 区分大小写 。比如 “Aa” 不能当做一个回文字符串。
示例 1:
输入:s = “abccccdd”
输出:7
解释:
我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
示例 2:
输入:s = “a”
输入:1
示例 3:
输入:s = “bb”
输入: 2
提示:
1 <= s.length <= 2000
s 只能由小写和/或大写英文字母组成
解题思路:
统计字符个数,偶数个字符可以分别放在两边,奇数个字符只能只用一种且一次,其中的偶数的字符放在两边,剩下的一个字符,放在中间当分界线。
代码实现:
class Solution:
def longestPalindrome(self, s: str) -> int:
# 统计字符串s的每个字符的个数
import collections
count = collections.Counter(s)
ans = 0
for value in count.values():
# 每次取偶数个字符分别放在回文串的两边
# ans记录的使用字符的个数,即回文串的长度
ans += value // 2 * 2
# 如果value是奇数的话,那么使用一次这样的奇数串
# 因为value中的偶数个字符可以放在回文串的两边
# 剩下的可以放在回文串的中间当做分界线
# 这样的个数是奇数的字符只能使用一种,否则就构不成回文串
if ans % 2 == 0 and value % 2 == 1:
ans += 1
return ans
边栏推荐
- 监听器 Listener
- Test function in pychram
- Pytorch training deep learning network settings CUDA specified GPU visible
- Design of serial port receiving data scheme
- Introduction to EtherCAT
- 报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
- 数据库中COMMENT关键字的使用
- The combination of applet container technology and IOT
- leetcode 1818 绝对值,排序,二分法,最大值
- Download and installation configuration of cygwin
猜你喜欢

岭回归和lasso回归

bootsrap中的栅格系统

服务器渲染技术jsp

pytorch nn. AdaptiveAvgPool2d(1)

用小程序的技术优势发展产业互联网

Nacos

How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars

The combination of applet container technology and IOT
![[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation](/img/b3/887d3fb64acbf3702814d32e2e6414.png)
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation

Blueprism registration, download and install -rpa Chapter 1
随机推荐
Leetcode 1818 absolute value, sorting, dichotomy, maximum value
JUC learning
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
Leetcode: offer 59 - I. maximum value of sliding window
Implement pow (x, n) function
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
Listener listener
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
SEM of C language_ Tvariable type
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
Pyramid scene parsing network [pspnet] thesis reading
File upload and download
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
Data exchange JSON
Download and installation configuration of cygwin
What happens when a function is called before it is declared in C?