当前位置:网站首页>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
边栏推荐
- 72. 编辑距离
- [TA frost wolf \u may- hundred people plan] 1.3 secret of texture
- 5. [WebGIS practice] software operation - service release and permission management
- Jeecgboot output log, how to use @slf4j
- 6. zigzag transformation
- 208. 实现 Trie (前缀树)
- [ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes
- 171. excel table column No
- 排序链表(归并排序)
- Leetcode 128 longest continuous sequence (hash set)
猜你喜欢

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

Feature pyramid networks for object detection
![[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st

不用加减乘除实现加法

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

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)

TEC: Knowledge Graph Embedding with Triple Context

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

Thread data sharing and security -threadlocal

实现pow(x,n)函数
随机推荐
Review column - message queue
409. 最长回文串
【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型
C语言的sem_t变量类型
Pyramid Scene Parsing Network【PSPNet】论文阅读
Idea plug-in backup table
静态库使用MFC和共享库使用MFC的区别
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
Database DDL (data definition language) knowledge points
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
168. excel table column name
ASGNet论文和代码解读2
Leetcode:829. Sum of continuous integers
Appium自动化测试基础--补充:C/S架构和B/S架构说明
Nacos
Future of NTF and trends in 2022
idea插件备份表