forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_409.java
29 lines (28 loc) · 860 Bytes
/
_409.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
package com.fishercoder.solutions;
public class _409 {
public static class Solution1 {
public int longestPalindrome(String s) {
int[] counts = new int[56];
for (char c : s.toCharArray()) {
if (Character.isUpperCase(c)) {
counts[c - 'A' + 33]++;
} else {
counts[c - 'a']++;
}
}
boolean hasOdd = false;
int len = 0;
for (int i = 0; i < 56; i++) {
if (counts[i] % 2 != 0) {
hasOdd = true;
if (counts[i] > 1) {
len += counts[i] - 1;
}
} else {
len += counts[i];
}
}
return hasOdd ? len + 1 : len;
}
}
}