-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path409.java
More file actions
61 lines (54 loc) · 1.77 KB
/
409.java
File metadata and controls
61 lines (54 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.HashMap;
import java.util.Map;
/**
* 409. Longest Palindrome
*
* Given a string s which consists of lowercase or uppercase letters,
* return the length of the longest palindrome that can be built with those letters.
*
* Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
*/
class Solution {
public int optimalSolution(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;
}
public int mySolution(String s) {
// create a HashMap of counts
HashMap<Character, Integer> counts = new HashMap<>();
for (char ch : s.toCharArray()) {
counts.put(ch, counts.getOrDefault(ch, 0) + 1);
}
// find the character with the largest odd count
int maxOddNumber = -1;
char maxOddChar = ' ';
for (Map.Entry<Character, Integer> e : counts.entrySet()) {
if (e.getValue() % 2 != 0 && e.getValue() > maxOddNumber) {
maxOddNumber = e.getValue();
maxOddChar = e.getKey();
}
}
// start counting
int length = 0;
for (Map.Entry<Character, Integer> e : counts.entrySet()) {
if (e.getValue() % 2 != 0) {
if (e.getKey() == maxOddChar) {
length += e.getValue();
} else if (e.getValue() > 1) {
length += e.getValue() - 1;
}
} else {
length += e.getValue();
}
}
return length;
}
}