import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static ArrayList permutation(String s) { // The result ArrayList res = new ArrayList(); // If input string's length is 1, return {s} if (s.length() == 1) { res.add(s); } else if (s.length() > 1) { int lastIndex = s.length() - 1; // Find out the last character String last = s.substring(lastIndex); // Rest of the string String rest = s.substring(0, lastIndex); // Perform permutation on the rest string and // merge with the last character res = merge(permutation(rest), last); } return res; } public static ArrayList merge(ArrayList list, String c) { ArrayList res = new ArrayList(); for (String s : list) { for (int i = 0; i <= s.length(); ++i) { String ps = new StringBuffer(s).insert(i, c).toString(); res.add(ps); } } return res; } private static int subPal(String str) { String s1 = ""; int maxLength = -1; int N = str.length(), count = 0; List permutatedStrings = permutation(str); Set palindromeArray = new HashSet(); for(String permutatedString: permutatedStrings){ for (int i = 2; i <= N; i++) { for (int j = 0; j <= N; j++) { int k = i + j - 1; if (k >= N) continue; s1 = permutatedString.substring(j, i + j); if (s1.length()>0 && s1.equals(new StringBuilder(s1).reverse().toString())) { palindromeArray.add(s1); if(maxLength