当前位置:网站首页>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;
}
边栏推荐
- MySQL main query and sub query
- Bicolor case
- Three paradigms of MySQL
- Vagrant virtual machine installation, disk expansion and LAN access tutorial
- Explanation of sentinel series' features, composition and deployment
- JNDI configuration for tongweb7
- Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
- Tongweb crawl performance log script
- 13 cancelendevent of a flowable end event and compensationthrowing of a compensation event
- How slow is the application system on tongweb? How dead is it?
猜你喜欢
Mongodb multi field aggregation group by
One of PowerShell optimizations: prompt beautification
How slow is the application system on tongweb? How dead is it?
Basic operations of MySQL auto correlation query
How to view tongweb logs correctly?
890. Find and Replace Pattern
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
Web site learning and sorting
OpenGL马赛克(八)
Wampserver (MySQL) installation
随机推荐
Agile conflicts and benefits
为什么那么多人讨厌A-Spice
Tongweb customs clearance guidelines
Sentinel series introduction to service flow restriction
Anaconda configuring the mirror source
Exception after repeated application redeployment on tongweb: application instance has been stopped already or outofmemoryerror:metaspace
A simple recursion problem of linked list
Mysql database crud operation
Four shardingsphere JDBC sharding strategies
A fast week
C calls the API and parses the returned JSON string
Difference between deviation and variance in deep learning
OpenGL mosaic (VIII)
Bicolor case
powershell优化之一:提示符美化
@Detailed explanation of propertysource usage method and operation principle mechanism
MySQL installation in Linux Environment
Some methods of string
使用cmake交叉编译helloworld
The 13th week of the second semester of sophomore year