做题博客链接
https://blog.youkuaiyun.com/qq_43349112/article/details/108542248
题目链接
https://leetcode-cn.com/problems/longest-palindrome/
描述
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。
在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
注意:
假设字符串的长度不会超过 1010。
示例
输入:
"abccccdd"
输出:
7
解释:
我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
初始代码模板
class Solution {
public int longestPalindrome(String s) {
}
}
代码
class Solution {
public int longestPalindrome(String s) {
int[] arr = new int[128];
for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i)]++;
}
int res = 0;
for (int i = 0; i < arr.length; i++) {
if ((arr[i] & 1) == 1) {
res += arr[i] - 1;
if ((res & 1) == 0) res++;
} else {
res += arr[i];
}
}
return res;
}
}
本文介绍如何使用Python解决LeetCode上的最长回文串问题,提供详细步骤和初始代码模板,并分享了优化后的解决方案。通过实例演示,助你理解如何构建并查找字符串中最长的回文子串,无论大小写敏感。
403

被折叠的 条评论
为什么被折叠?



