Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Scanner;
- public class teet {
- public static void main(String args[]) {
- Scanner reader = new Scanner(System.in);
- int numTests = reader.nextInt();
- int[] results = new int [numTests];
- for (int i = 0; i < numTests; i++) {
- String str = reader.next();
- if (checkForPalindrome(str)) results[i] = str.length();
- else {
- results[i] = calculate(str);
- }
- }
- reader.close();
- for (int i = 0; i < numTests; i++) {
- System.out.println(results[i]);
- }
- }
- private static boolean checkForPalindrome(String str) {
- HashSet<Character> st = new HashSet<Character>();
- st.addAll(Arrays.asList('A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y'));
- for (int i = 0; i < str.length()/2 + 1; i++) {
- if (str.charAt(i) != str.charAt(str.length() - i - 1) || !st.contains(str.charAt(i))) {
- return false;
- }
- }
- return true;
- } // qwerty 6
- private static int calculate(String str) {
- int count = 0;
- for (int length = str.length(); length > 0; length--) {
- for (int start = 0; start < str.length() - length + 1 ; start++) {
- String tmp = str.substring(start, start + length);
- if (checkForPalindrome(tmp)) {
- return tmp.length();
- }
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement