当前位置:网站首页>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
边栏推荐
- Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
- 衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
- [TA frost wolf \u may - "hundred people plan"] 2.1 color space
- 5. [WebGIS practice] software operation - service release and permission management
- [ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes
- 在线公网安备案保姆级教程【伸手党福利】
- FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
- 详解Spark运行模式(local+standalone+yarn)
- 187. 重复的DNA序列
- 165. 比较版本号
猜你喜欢
Pytorch training deep learning network settings CUDA specified GPU visible
Valentine's Day is nothing.
pytorch训练深度学习网络设置cuda指定的GPU可见
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
Promql select time series
LeetCode 128最长连续序列(哈希set)
Explain spark operation mode in detail (local+standalone+yarn)
Appium自动化测试基础--补充:C/S架构和B/S架构说明
【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
随机推荐
392. 判断子序列
torch.histc
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
Appium自动化测试基础 — APPium基本原理
【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
pytorch训练深度学习网络设置cuda指定的GPU可见
Error: plug ins declaring extensions or extension points must set the singleton directive to true
The difference between MFC for static libraries and MFC for shared libraries
【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
Leetcode: offer 59 - I. maximum value of sliding window
10、Scanner.next() 无法读取空格/indexOf -1
Pathmeasure implements loading animation
【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型
Promql select time series
Download and installation configuration of cygwin
6. Z 字形变换
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
Appium自动化测试基础--补充:C/S架构和B/S架构说明
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Cygwin的下载和安装配置