Skip to content

Commit 88fffe6

Browse files
refactor 409
1 parent 6ed53e8 commit 88fffe6

File tree

1 file changed

+23
-42
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+23
-42
lines changed
Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,29 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 409. Longest Palindrome
5-
*
6-
* Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
7-
* This is case sensitive, for example "Aa" is not considered a palindrome here.
8-
*
9-
* Note:
10-
* Assume the length of given string will not exceed 1,010.
11-
*
12-
* Example:
13-
* Input:
14-
* "abccccdd"
15-
*
16-
* Output:
17-
* 7
18-
*
19-
* Explanation:
20-
* One longest palindrome that can be built is "dccaccd", whose length is 7.
21-
*/
223
public class _409 {
23-
public static class Solution1 {
24-
public int longestPalindrome(String s) {
25-
int[] counts = new int[56];
26-
for (char c : s.toCharArray()) {
27-
if (Character.isUpperCase(c)) {
28-
counts[c - 'A' + 33]++;
29-
} else {
30-
counts[c - 'a']++;
4+
public static class Solution1 {
5+
public int longestPalindrome(String s) {
6+
int[] counts = new int[56];
7+
for (char c : s.toCharArray()) {
8+
if (Character.isUpperCase(c)) {
9+
counts[c - 'A' + 33]++;
10+
} else {
11+
counts[c - 'a']++;
12+
}
13+
}
14+
boolean hasOdd = false;
15+
int len = 0;
16+
for (int i = 0; i < 56; i++) {
17+
if (counts[i] % 2 != 0) {
18+
hasOdd = true;
19+
if (counts[i] > 1) {
20+
len += counts[i] - 1;
21+
}
22+
} else {
23+
len += counts[i];
24+
}
25+
}
26+
return hasOdd ? len + 1 : len;
3127
}
32-
}
33-
boolean hasOdd = false;
34-
int len = 0;
35-
for (int i = 0; i < 56; i++) {
36-
if (counts[i] % 2 != 0) {
37-
hasOdd = true;
38-
if (counts[i] > 1) {
39-
len += counts[i] - 1;
40-
}
41-
} else {
42-
len += counts[i];
43-
}
44-
}
45-
return hasOdd ? len + 1 : len;
4628
}
47-
}
4829
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy