diff --git a/src/0409.longest-palindrome.409/solution.java b/src/0409.longest-palindrome.409/solution.java new file mode 100644 index 00000000..fac1e1e5 --- /dev/null +++ b/src/0409.longest-palindrome.409/solution.java @@ -0,0 +1,22 @@ +class Solution { + public int longestPalindrome(String s) { + int[] counts = new int[52]; + for(char c : s.toCharArray() ){ + if(Character.isUpperCase(c)){ + counts[c-'A']++; + }else{ + counts[c-'a'+26]++; + } + } + boolean hasOdd = false; + int result = 0; + for(int count:counts){ + if(count%2 == 1){ + hasOdd = true; + count--; + } + result += count; + } + return result + (hasOdd?1:0); + } +} \ No newline at end of file