fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static int longestPalindrome(String s) {
  11. int[] count = new int[128];
  12. for (char c: s.toCharArray())
  13. count[c]++;
  14.  
  15. int ans = 0;
  16. for (int v: count) {
  17. ans += v / 2 * 2;
  18. if (ans % 2 == 0 && v % 2 == 1)
  19. ans++;
  20. }
  21. return ans;
  22. }
  23.  
  24. public static void main (String[] args) throws java.lang.Exception
  25. {
  26. // your code goes here
  27. System.out.println("The length of longest palindrome is:- " + longestPalindrome("abccccdd"));
  28. }
  29. }
Success #stdin #stdout 0.09s 27740KB
stdin
Standard input is empty
stdout
The length of longest palindrome is:- 7