Skip to content

Commit

Permalink
409
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangshanmeta committed Dec 12, 2024
1 parent 0991da7 commit 7fa236f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/0409.longest-palindrome.409/solution.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 7fa236f

Please sign in to comment.