-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestPalindromeLength.java
40 lines (32 loc) · 1.54 KB
/
LongestPalindromeLength.java
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
public class LongestPalindromeLength {
/**A palindrome consists of letters with equal partners, plus possibly a unique center (without a partner). The letter i from the left has its partner i from the right. For example in 'abcba', 'aa' and 'bb' are partners, and 'c' is a unique center.
Imagine we built our palindrome. It consists of as many partnered letters as possible, plus a unique center if possible. This motivates a greedy approach.
Algorithm
For each letter, say it occurs v times. We know we have v // 2 * 2 letters that can be partnered for sure. For example, if we have 'aaaaa', then we could have 'aaaa' partnered, which is 5 // 2 * 2 = 4 letters partnered.
At the end, if there was any v % 2 == 1, then that letter could have been a unique center. Otherwise, every letter was partnered. To perform this check, we will check for v % 2 == 1 and ans % 2 == 0, the latter meaning we haven't yet added a unique center to the answer.
*
*
*
*
*/
public static int longestPalindrome(String s) {
int[] count = new int[128];
for (char c: s.toCharArray())
count[c]++;
int ans = 0;
for (int v: count) {
if(v==5){
System.out.println();
}
ans +=(v / 2) * 2;
if (ans % 2 == 0 && v % 2 == 1)
ans++;
}
return ans;
}
public static void main(String[] args) {
String s = "aaaaabn";
int len = longestPalindrome(s);
System.out.println(len);
}
}