Skip to content

Commit 7885ed9

Browse files
authored
Create LongestPalindrome.java
1 parent 67abb3e commit 7885ed9

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

easy/LongestPalindrome.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//409. Longest Palindrome
2+
class Solution {
3+
public int longestPalindrome(String s) {
4+
int ans = 0;
5+
int[] count = new int[128];
6+
7+
for (final char c : s.toCharArray())
8+
++count[c];
9+
10+
for (final int c : count)
11+
ans += c % 2 == 0 ? c : c - 1;
12+
13+
final boolean hasOddCount = Arrays.stream(count).anyMatch(c -> c % 2 == 1);
14+
15+
return ans + (hasOddCount ? 1 : 0);
16+
}
17+
}

0 commit comments

Comments
 (0)