LeetCode_409. Longest Palindrome
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.
package leetcode.easy;
public class LongestPalindrome {
public int longestPalindrome(String s) {
int[] count = new int[128];
for (char c : s.toCharArray()) {
count[c]++;
}
int ans = 0;
for (int v : count) {
ans += v / 2 * 2;
if (ans % 2 == 0 && v % 2 == 1) {
ans++;
}
}
return ans;
}
@org.junit.Test
public void test() {
System.out.println(longestPalindrome("abccccdd"));
}
}
LeetCode_409. Longest Palindrome的更多相关文章
- 【leetcode】409. Longest Palindrome
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- LeetCode 409 Longest Palindrome
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- LeetCode Longest Palindrome
原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...
- 每天一道LeetCode--409 .Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 24. leetcode 409. Longest Palindrome
409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...
- [Swift]LeetCode409. 最长回文串 | Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode&Python] Problem 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
随机推荐
- 关于struct和typedef struct
以 struct TelPhone{ ]; ]; }; 为例 这里先定义了一个 TelPhone的结构体. 加入需要为TelPhone定义一个别名: 其语法为 typedef TelPhone TP: ...
- 使用Xpath爬虫库下载诗词名句网的史书典籍类所有文章。
# 需要的库 from lxml import etree import requests # 请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows ...
- Parity game(带权并查集+离散化)
题目链接 //kuangbin 题意: 现在你和你的朋友正在玩一种游戏. 你的朋友写下一串0和1的序列,然后你选择其中一串子序列(如[3,5])并且问他这个序列是包含奇数个1还是偶数个1(和是奇数还 ...
- C# 退出应用程序的几种方法
Application.Exit();//好像只在主线程可以起作用,而且当有线程,或是阻塞方法的情况下,很容易失灵 this.Close();//只是关闭当前窗体. Application.ExitT ...
- Python中实现count(distinct )
假设一个表有6个字段c1,c2,c3,c4,c5,c6,有如下的sql语句: select c1,count(distinct(c6)) from tbl where c3>1 group by ...
- [NOIp 2018]all
Description 题库链接: Day1 T1 铺设道路 Day1 T2 货币系统 Day1 T3 赛道修建 Day2 T1 旅行 Day2 T2 填数游戏 Day2 T3 保卫王国 Soluti ...
- Hibernate对象持久化的三种状态
1.三种状态: public static void testSel() { Session session = HibernateUtils.openSession(); Transaction t ...
- 开源项目 02 HttpLib
using JumpKick.HttpLib; using Newtonsoft.Json; using System; using System.Collections.Generic; using ...
- C博客作业01--分支丶顺序结构
1.本章学习总结 1.1学习内容总结 分支结构 if else-if语句与switch语句都具有选择判断的功能,但是在使用时又有所区别,按题目的不同要求与题意选择不同语句. if else-if语句表 ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...