亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LintCode] Longest Palindrome

nicercode / 2619人閱讀

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.

Notice

Assume the length of given string will not exceed 1010.

Example

Given s = "abccccdd" return 7

One longest palindrome that can be built is "dccaccd", whose length is 7.

Solution
public class Solution {
    /*
     * @param s: a string which consists of lowercase or uppercase letters
     * @return: the length of the longest palindromes that can be built
     */
    public int longestPalindrome(String s) {
        // write your code here
        int res = 0;
        //Use HashMap to store occurrence, when it"s even, res adds 2 and map deletes the key
        Map map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (map.containsKey(ch)) {
                map.remove(ch);
                res += 2;
            } else {
                map.put(ch, 1);
            }
        }
        if (res < s.length()) res += 1;
        return res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/68259.html

相關(guān)文章

  • [LintCode/LeetCode] Longest Palindrome Substring

    摘要:是左閉右開(kāi)區(qū)間,所以要。,要理解是和之間只有一個(gè)元素。循環(huán)每次的時(shí)候,都要更新子串更大的情況。補(bǔ)一種中點(diǎn)延展的方法循環(huán)字符串的每個(gè)字符,以該字符為中心,若兩邊為回文,則向兩邊繼續(xù)延展。循環(huán)返回長(zhǎng)度最長(zhǎng)的回文串即可。 Problem Given a string S, find the longest palindromic substring in S. You may assume ...

    AaronYuan 評(píng)論0 收藏0
  • [Leetcode]Longest Palindrome

    摘要:解題思路我們發(fā)現(xiàn)結(jié)果其實(shí)就是字符的偶數(shù)個(gè)數(shù)是否有單一的字符,如果有就加把單一字符放在回文中間,如果沒(méi)有就加字母區(qū)分大小寫,代碼 Longest PalindromeGiven a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that...

    beita 評(píng)論0 收藏0
  • leetcode409.Longest Palindrome

    摘要:題目要求輸入一個(gè)字符串,計(jì)算用這個(gè)字符串中的值構(gòu)成一個(gè)最長(zhǎng)回?cái)?shù)的長(zhǎng)度是多少。直觀來(lái)看,我們立刻就能想到統(tǒng)計(jì)字符串中每個(gè)字符出現(xiàn)的次數(shù),如果該字符出現(xiàn)次數(shù)為偶數(shù),則字符一定存在于回?cái)?shù)中。這個(gè)細(xì)節(jié)需要注意。 題目要求 Given a string which consists of lowercase or uppercase letters, find the length of the...

    linkin 評(píng)論0 收藏0
  • [LeetCode/LintCode] Largest Palindrome Product

    Problem Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example Input: 2Output: 987Ex...

    Barry_Ng 評(píng)論0 收藏0
  • [LeetCode/LintCode] Valid Palindrome

    Valid Palindrome Problem Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Example A man, a plan, a canal: Panama is a palindrome. race a ca...

    Ververica 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<