Re: [闲聊] 每日leetcode

楼主: sustainer123 (caster)   2024-06-04 09:42:31
https://leetcode.com/problems/longest-palindrome
409. Longest Palindrome
给定一字串s 回传用s的字母能建立的最长回文字串的长度
Example 1:
Input: s = "abccccdd"
Output: 7
Explanation: One longest palindrome that can be built is "dccaccd", whose
length is 7.
Example 2:
Input: s = "a"
Output: 1
Explanation: The longest palindrome that can be built is "a", whose length is
1.
Constraints:
1 <= s.length <= 2000
s consists of lowercase and/or uppercase English letters only.
思路:
哈希表计数
Python Code:
class Solution:
def longestPalindrome(self, s: str) -> int:
record = defaultdict(int)
for n in s:
record[n] += 1
count = 0
one = 1
for v in record.values():
if v % 2 == 1:
if one > 0:
one -= 1
else:
count += 1
return len(s) - count
又是ez的一天 舒服
作者: yam276 ('_')   2024-06-04 09:44:00
什么!
楼主: sustainer123 (caster)   2024-06-04 09:44:00
什么!
作者: DJYOSHITAKA (Evans)   2024-06-04 09:46:00
大湿
作者: SecondRun (雨夜琴声)   2024-06-04 09:51:00
大师
作者: JIWP (JIWP)   2024-06-04 09:55:00
别卷了
作者: orangeNoob (橘子色的肥肥)   2024-06-04 10:50:00
别卷了

Links booklink

Contact Us: admin [ a t ] ucptt.com