Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. package test;
  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4. import java.util.Scanner;
  5. public class teet {
  6. public static void main(String args[]) {
  7. Scanner reader = new Scanner(System.in);
  8. int numTests = reader.nextInt();
  9. int[] results = new int [numTests];
  10. for (int i = 0; i < numTests; i++) {
  11. String str = reader.next();
  12. if (checkForPalindrome(str)) results[i] = str.length();
  13. else {
  14. results[i] = calculate(str);
  15. }
  16. }
  17. reader.close();
  18. for (int i = 0; i < numTests; i++) {
  19. System.out.println(results[i]);
  20. }
  21. }
  22. private static boolean checkForPalindrome(String str) {
  23. HashSet<Character> st = new HashSet<Character>();
  24. st.addAll(Arrays.asList('A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y'));
  25. for (int i = 0; i < str.length()/2 + 1; i++) {
  26. if (str.charAt(i) != str.charAt(str.length() - i - 1) || !st.contains(str.charAt(i))) {
  27. return false;
  28. }
  29. }
  30. return true;
  31. } // qwerty 6
  32. private static int calculate(String str) {
  33. int count = 0;
  34. for (int length = str.length(); length > 0; length--) {
  35. for (int start = 0; start < str.length() - length + 1 ; start++) {
  36. String tmp = str.substring(start, start + length);
  37. if (checkForPalindrome(tmp)) {
  38. return tmp.length();
  39. }
  40. }
  41. }
  42. return count;
  43. }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement