给出一个由小写或大写字母组成的字符串,找到可以用这些字母建立的最长回文的长度。
这是区分大小写的,例如“ Aa”在这里不被视为回文。
注意:
假定给定字符串的长度不超过1,010。
示例:
输入:"abccccdd"
输出:7
说明:
可以建立的最长回文是“ dccaccd”,其长度为7。
我的代码可用于简单的输入,例如"abccccdd"
和"banana"
,但对"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"
无效。我不确定如何调试它。
class Solution {
public int longestPalindrome(String s) {
Map<Character, Integer> map = new HashMap<>();
char[] carr = s.toCharArray();
Arrays.sort(carr);
int leftInd = 0;
int rightInd = 0;
for(int i=0; i<carr.length; i++){
if(map.containsKey(carr[i]))
continue;
else
map.put(carr[i], 1);
}
for(int i=0; i<carr.length-1; i++){
for(int j=i+1; j<carr.length; j++){
if(carr[i]==carr[j]){
if(map.get(carr[i])==null)
continue;
carr[j] = Character.MIN_VALUE;
int count = map.get(carr[i]);
map.put(carr[i], count + 1);
}
}
}
int ans = 0;
int[] oddValArr = new int[map.size()];
int oddInd = 0;
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
Character key = entry.getKey();
Integer value = entry.getValue();
if(value % 2 == 0){
ans += value;
}
else{
oddValArr[oddInd] = value;
oddInd++;
}
}
int biggestOddNum = 0;
for(int i=0; i<oddValArr.length; i++){
if(oddValArr[i] > biggestOddNum)
biggestOddNum = oddValArr[i];
}
return ans + biggestOddNum;
}
}
输出 655
预期 983
答案 0 :(得分:1)
您在这里的错误是,您在oddValArr
中仅使用最大的奇数组。例如,如果输入为“ aaabbb”,则最大回文为“ abbba”,因此组 a 的长度为3,这是一个奇数,我们使用3 - 1 = 2
个字母
此外,可以使用Map将这些嵌套的for
循环替换为一个for
:
public int longestPalindrome(String s) {
Map<Character, Integer> map = new HashMap<>(); // letter groups
for(int i=0; i<s.length(); i++){
char c = s.charAt(i));
if(map.containsKey(c))
map.put(c, map.get(i) + 1);
else
map.put(c, 1);
}
boolean containsOddGroups = false;
int ans = 0;
for(int count : map.values()){
if(count % 2 == 0) // even group
ans += count;
else{ // odd group
containsOddGroups = true;
ans += count - 1;
}
}
if(!containOddGroups)
return ans;
else
return ans + 1; // we can place one letter in the center of palindrome
}
答案 1 :(得分:0)
您快到了,但是已经使它复杂了很多。我的解决方案是几乎只从解决方案中删除代码:
public static int longestPalindrome(String s) {
Map<Character, Integer> map = new HashMap<>();
char[] carr = s.toCharArray();
for (int i = 0; i < carr.length; i++) {
if (map.containsKey(carr[i]))
map.put(carr[i], map.get(carr[i]) + 1);
else
map.put(carr[i], 1);
}
int ans = 0;
int odd = 0;
for (Integer value : map.values()) {
if (value % 2 == 0) {
ans += value;
} else {
ans += value - 1;
odd = 1;
}
}
return ans + odd;
}
说明:
ans
count - 1
个字符用于长度相等的回文中