「LeetCode每日一题」——409. 最长回文串
409. 最长回文串
409. 最长回文串
链接:https://leetcode-cn.com/problems/longest-palindrome/
难度:简单
题目
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。
在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
注意:
假设字符串的长度不会超过 1010。
示例 1:
输入:
"abccccdd"
输出:
7
解释:
我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
思路
根据题意,这里回文串满足的是字符出现了两次以及两次以上,最长回文串就是字符出现两次以上的字符,然后在有条件的情况下加1个单字符。这个情况包括了"bb"和"aacccccbb"的形式,我们加了判断,具体代码见方案解法。
方案代码
方案解法:
from collections import Counter
class Solution:
def longestPalindrome(self, s: str) -> int:
res = 0
c = Counter(s)
for i in c.values():
res += i // 2 * 2
if res % 2 == 0 and i % 2 == 1:
res += 1
return res
相关
原创文章,作者:flypython,如若转载,请注明出处:http://flypython.com/algorithm/leetcode/238.html
相关推荐
-
「LeetCode每日一题」—— LOCF.51. 数组中的逆序对
LOCF.51. 数组中的逆序对 链接:https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/难度:中等 …
24/04/2020 -
「LeetCode每日一题」—— 11. 盛最多水的容器
11. 盛最多水的容器 链接:https://leetcode-cn.com/problems/container-with-most-water/难度:中等 题目 思路 这道题和…
18/04/2020 -
「LeetCode每日一题」——1103. 分糖果 II
1103. 分糖果 II 链接:https://leetcode-cn.com/problems/distribute-candies-to-people/难度:简单 题目 排排坐…
05/03/2020 -
「LeetCode每日一题」——994. 腐烂的橘子
994. 腐烂的橘子 链接:https://leetcode-cn.com/problems/rotting-oranges/难度:简单 题目 在给定的网格中,每个单元格可以有以下…
04/03/2020 -
「LeetCode每日一题」—— 151. 翻转字符串里的单词
151. 翻转字符串里的单词 链接:https://leetcode-cn.com/problems/reverse-words-in-a-string/难度:中等 题目 给定一个…
10/04/2020 -
「LeetCode每日一题」—— 56. 合并区间
56. 合并区间 链接:https://leetcode-cn.com/problems/merge-intervals/难度:中等 题目 给出一个区间的集合,请合并所有重叠的区间…
16/04/2020 -
「LeetCode每日一题」—— 445. 两数相加 II
445. 两数相加 II 链接:https://leetcode-cn.com/problems/add-two-numbers-ii/难度:中等 题目 点击原文链接跳转查看题目 …
14/04/2020 -
「LeetCode每日一题」——LCCI 10.01. 合并排序的数组
LCCI 10.01. 合并排序的数组 链接:https://leetcode-cn.com/problems/sorted-merge-lcci/难度:简单 题目 给定两个排序后…
03/03/2020 -
「LeetCode每日一题」—— 46. 全排列
46. 全排列 链接:https://leetcode-cn.com/problems/permutations/难度:中等 题目 点击原文链接跳转查看题目 思路 这题算是一个基础…
25/04/2020 -
「LeetCode每日一题」—— LCOF.13. 机器人的运动范围
LCOF.13. 机器人的运动范围 题目链接:https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof…
08/04/2020
您必须登录才能发表评论。