LeetCode——409. Longest Palindrome

题目:

  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.

大意:

  给出一个由小写或大写字母组成的字符串,找到能被其中的字母组成的最长的回文的长度。

  这是区分大小写的,比如“Aa”就不能认为是回文。

  注意: 
  假设给出的字符串长度不会超过1010。

  例子:

输入:
“abccccdd”

输出:
7

解释:
最长的回文为“dccaccd”,长度为7。

  回文就是中间有一个单个的字母,两边的字母是对称的。

  • aaadd的最长回文是daaad
  • aaadddcc的最长回文是cdaaadc或者cadddac

  可以总结为:

  • 数目为偶数的字母将全部出现在最长回文中,
  • 奇数就有两种情况,有没有大于1个的,如果有大于1个的。就比如有3个,那么回文长度将加上2。如果有7个就加上6.
  • 如果所有的奇数都不大于1,最后结果再加1即可
class Solution {
    public int longestPalindrome(String s) {
        int result = 0;
        boolean flag = false;
        int[] a = new int[26*2];
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) < ‘a‘) {
                a[s.charAt(i) - ‘A‘ + 26]++;
            } else {
                a[s.charAt(i) - ‘a‘] ++;
            }
        }
        for(int i:a){
            if(i == 1){
                flag = true;
            }
            if(i > 1){
                result += i / 2 * 2;
                if (i % 2 == 1) flag = true;
            }
        }
        if(flag){
            result++;
        }
        return result;
    }}

  这里使用一个26*2的数组记录字幕个数,因为是区分大小写的,每次如果字母数目大于1先除2再乘2,这样偶数不变,奇数将减一。还设置了一个flag变量判断是否存在奇数。

原文地址:https://www.cnblogs.com/xxbbtt/p/8378043.html

时间: 2024-07-29 05:45:00

LeetCode——409. Longest Palindrome的相关文章

LeetCode 409 Longest Palindrome

Problem: 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. Assume the l

[LeetCode&amp;Python] Problem 409. Longest Palindrome

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 lengt

409. Longest Palindrome

题目: 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 l

409. Longest Palindrome(计算一组字符集合可以组成的回文字符串的最大长度)

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 lengt

letecode [409] - Longest Palindrome

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 lengt

LeetCode #5 Longest Palindrome

Description Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Sample Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Input: "cbbd" O

LeetCode the longest palindrome substring

回文检测,参考http://blog.csdn.net/feliciafay/article/details/16984031 使用时间复杂度和空间复杂度相对较低的动态规划法来检测,具体的做法 图一 偶数个回文字符情况 图二 奇数个回文字符情况 核心就是如果一个子串是回文,如果分别向回文左右侧扩展一个字符相同,那么回文就向外扩展一位. 实现的代码如下 bool table[1000][1000] = {false}; int sStart = 0; int iLength = 1; int n

Leetcode 之 Longest Palindrome

#include <cstdio> #include <algorithm> int MAX_SIZE = 100000; int gen_odd_str(const char * str, char * odd_str) { int i = 0; for (i = 0; *str != 0 ; ++i, ++str) { odd_str[2 * i] = '#'; odd_str[2 * i + 1] = *str; } odd_str[2 * i] = '#'; odd_str

LeetCode Longest Palindrome

原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: 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&qu