当前位置:网站首页>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
边栏推荐
猜你喜欢
![Pyramid scene parsing network [pspnet] thesis reading](/img/05/4645c8a595083479dee6835620335d.png)
Pyramid scene parsing network [pspnet] thesis reading

[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API

排序链表(归并排序)

Research on target recognition and tracking based on 3D laser point cloud

Blueprism registration, download and install -rpa Chapter 1

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)

Complete knapsack problem

【TA-霜狼_may-《百人计划》】2.3 常用函数介绍

小程序容器技术与物联网IoT的结合点

Take you through a circuit board, from design to production (dry goods)
随机推荐
10. 正则表达式匹配
静态库使用MFC和共享库使用MFC的区别
【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
187. 重复的DNA序列
TEC: Knowledge Graph Embedding with Triple Context
318. 最大单词长度乘积
5. [WebGIS practice] software operation - service release and permission management
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Review column - message queue
Promql select time series
AfxMessageBox和MessageBox的用法
5、【WebGIS实战】软件操作篇——服务发布及权限管理
389. 找不同
torch.histc
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
[daily training] 1175 Prime permutation
214. minimum palindrome string
Leetcode: offer 59 - I. maximum value of sliding window
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
【TA-霜狼_may-《百人计划》】1.3纹理的秘密