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" 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.

题解:

Time Complexity: O(s.length()). Space: O(s.length()).

AC Java:

 1 public class Solution {
 2     public int longestPalindrome(String s) {
 3         if(s == null || s.length() == 0){
 4             return 0;
 5         }
 6
 7         int res = 0;
 8         HashSet<Character> hs = new HashSet<Character>();
 9         for(int i = 0; i<s.length(); i++){
10             char c = s.charAt(i);
11             if(hs.contains(c)){
12                 hs.remove(c);
13                 res++;
14             }else{
15                 hs.add(c);
16             }
17         }
18         return hs.isEmpty() ? res*2 : res*2+1;
19     }
20 }

类似Palindrome Permutation.

时间: 2024-08-23 23:26:11

LeetCode Longest Palindrome的相关文章

[LeetCode] 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 leng

[LeetCode] Longest Palindrome Substring 具体分析

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 面DP题的考官都是神经病. .(吐槽) 貌似挺easy出回文的题,那么今天细致分析下DP的做法!! 以解我被DP问题虐成渣渣的心碎感觉.如有错误请指出

[LeetCode] Longest Palindrome Substring 详细分析

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 面DP题的考官都是神经病..(吐槽) 貌似挺容易出回文的题,那么今天仔细分析下DP的做法!!以解我被DP问题虐成渣渣的心碎感觉.如有错误请指出~ 首先

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——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

[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

[LeetCode] Longest Palindromic Substring [14]

题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length ofS is 1000, and there exists one unique longest palindromic substring. 原题链接 解题思路 最长回文字串,相信做过Palindrome Partitioning II 这个题的同学应该可以很快做出来.没错,这个题还可以

LeetCode: Longest Substring Without Repeating Characters 题解

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst