|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
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 |
| - */ |
22 | 3 | 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; |
31 | 27 | }
|
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; |
46 | 28 | }
|
47 |
| - } |
48 | 29 | }
|
0 commit comments