Leetcode: Longest Palindrome
LeetCode Longest Palindrome
2023-09-11 14:14:07 时间
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note: Assume the length of given string will not exceed 1,010. Example: Input: "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
1 public class Solution { 2 public int longestPalindrome(String s) { 3 if (s==null || s.length()==0) return 0; 4 int[] count = new int[52]; 5 for (int i=0; i<s.length(); i++) { 6 char c = s.charAt(i); 7 if (c < 'a') count[c-'A']++; 8 else count[c-'a'+26]++; 9 } 10 int countOdd = 0; 11 for (int each : count) { 12 if (each % 2 == 1) countOdd++; 13 } 14 return countOdd>0? s.length()-countOdd+1 : s.length(); 15 } 16 }
相关文章
- Leetcode 之Longest Palindromic Substring(30)
- Java实现 LeetCode 809 情感丰富的文字 (暴力)
- Java实现 LeetCode 617 合并二叉树(遍历树)
- Java实现 LeetCode 567 字符串的排列(滑动窗口,处理区间内的字符数量)
- Java实现 LeetCode 528 按权重随机选择(TreeMap)
- Java实现 LeetCode 275 H指数 II
- Java实现 LeetCode 232 用栈实现队列
- Java实现 LeetCode 72 编辑距离
- Java实现 LeetCode 61 旋转链表
- Java实现 LeetCode 30 串联所有单词的子串
- 【LeetCode算法-14】Longest Common Prefix
- 每日一道 LeetCode (11):外观数列
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
- 【LeetCode算法-28/35】Implement strStr()/Search Insert Position
- 【LeetCode算法-14】Longest Common Prefix
- [LeetCode] Word Pattern
- [LeetCode] Longest Substring Without Repeating Characters
- 【贪心】LeetCode 11. 盛最多水的容器【中等】
- LeetCode-2024. 考试的最大困扰度
- Python 刷Leetcode题库,顺带学英语单词(22)
- 【LeetCode 13】罗马数字转整数
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
- 【LeetCode OJ 14】Longest Common Prefix
- Leetcode 953. 验证外星语词典(感觉还挺难的)
- [LeetCode] Longest Substring Without Repeating Characters
- Leetcode-3 Longest Substring Without Repeating Characters
- leetcode 409. Longest Palindrome
- leetcode 521. Longest Uncommon Subsequence I