当前位置:网站首页>Leetcode- longest palindrome string - simple
Leetcode- longest palindrome string - simple
2022-06-13 05:49:00 【AnWenRen】
title :409 Longest palindrome - Simple
subject
Given a string of uppercase and lowercase letters , Find the longest palindrome string constructed from these letters .
In the process of construction , Please pay attention to case sensitivity . such as
"Aa"Can't be treated as a palindrome string .Be careful :
Suppose that the length of the string does not exceed 1010.
Example 1
Input :
"abccccdd"
Output :
7
explain :
The longest palindrome string we can construct is "dccaccd", Its length is 7.
Code Java
// Sort Find two similarities - 2ms
public int longestPalindrome(String s) {
int result = 0;
char[] chars = s.toCharArray();
Arrays.sort(chars);
for (int i = 1; i < chars.length; i++) {
if (chars[i] == chars[i-1]) {
i++;
result += 2;
}
}
if (result < chars.length)
return result + 1;
else return result;
}
// Number of hash stores - Less memory usage But it's still slow
public int longestPalindrome1(String s) {
int[] big = new int[26];
int[] small = new int[26];
int result = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch >= 'a') {
small[ch - 97] ++;
} else {
big[ch - 65] ++;
}
}
for (int i = 0; i < 26; i++) {
result += big[i] / 2 * 2;
result += small[i] / 2 * 2;
}
if (result < s.length())
return result + 1;
else return result;
}
// Optimize array
public int longestPalindrome2(String s) {
int[] count = new int[128];
int length = s.length();
for (int i = 0; i < length; ++i) {
char c = s.charAt(i);
count[c]++;
}
int ans = 0;
for (int v: count) {
ans += v / 2 * 2;
}
if (ans < length)
return ans + 1;
return ans;
}
边栏推荐
- How MySQL optimizes the use of joint index ABC
- [automated test] cypress manual
- Integration of sentinel series Nacos to realize rule synchronization and persistence
- 18 flowable task manualtask and receivetask
- Solve the problem of garbled code in the MySQL execution SQL script database in docker (no need to rebuild the container)
- Parallelgateway and exclusivegateway of 14 gateways
- 1 Introduction to drools rule engine (usage scenarios and advantages)
- Standard input dialog for pyqt5 qinputdialog
- Working principle of sentinel series (concept)
- Hump naming and underlining
猜你喜欢

Three paradigms of MySQL

MySQL built-in functions

12 error end event and terminateendevent of end event

AUTOSAR actual combat tutorial pdf version

How slow is the application system on tongweb? How dead is it?

MongoDB 多字段聚合Group by

890. Find and Replace Pattern

Wampserver (MySQL) installation

Agile conflicts and benefits

Working principle of sentinel series (concept)
随机推荐
Web site learning and sorting
Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience
Concurrent programming -- source code analysis of thread pool
Sentinel series introduction to service flow restriction
Concurrent programming -- countdownlatch combined with thread pool
MySQL advanced query
Automatic database backup (using Navicat)
= = relation between int and integer
2021.9.29 learning log MIME type
18 flowable task manualtask and receivetask
若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
How to view tongweb logs correctly?
MySQL transactions and foreign keys
Etcd understanding of microservice architecture
MongoDB 多字段聚合Group by
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
移动端适配方案
Let's talk about how ArrayList is dynamically resized and what kind of mechanism is it?
ArrayList loop removes the pit encountered
Explanation of sentinel series' features, composition and deployment