How to improve the performance of this code, reducing the compile time and keeping the functionality of the code same ?
The code is to extract two sub-strings from different strings and concatinating them to provide the largest possible palindromic string.
the Question was :You have two strings, (a) and (b). Find a string, (c), such that: (c)=(d)+(e).
(d),(e) can be expressed as where (d) is a non-empty substring of (a) and (e) is a non-empty substring of (b). (c) is a palindromic string. The length of is as long as possible. For each of the pairs of strings (a) and (b) received as input, find and print string on a new line. If you're able to form more than one valid string , print whichever one comes first alphabetically. If there is no valid answer, print -1 instead.
import java.io.*;
import java.util.*;
public class Solution {
    boolean isPalindrome(String s) {
  int n = s.length();
  for (int i=0;i<(n / 2);++i) {
     if (s.charAt(i) != s.charAt(n - i - 1)) {
         return false;
     }
  }
  return true;
}
    public static void main(String[] args) {
        String result="";
         Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int a=0; a<n; a++)
            {int length1, length2, i,c,d,j;
        int max_length=0;
        String string1 = in.next();
        String sub1,sub2;
        String string2 = in.next();
        length2=string2.length();
        length1 = string1.length();   
      for( c = 0 ; c <length1 ; c++ )
      {
         for( i = length1-c ; i >0 ; i-- )
         {
            sub1 = string1.substring(c, c+i);
            for( d = 0 ; d < length2 ; d++ )
      {
         for( j = length2-d ; j >0 ; j-- )
         {
            sub2 = string2.substring(d, d+j);
            String temp=sub1+sub2;
              Solution obj= new Solution();
             if(temp.length()>=max_length && obj.isPalindrome(temp)==true)
                 {
                 if (max_length==temp.length())
                  {   if(temp.compareTo(result)<0)
                     {
                     result=temp;
                  }}
                 else {
                     max_length=temp.length();
                 result=temp;
                  }
             }
         }
      }
         }
      }
             if(max_length==0)
                 System.out.println(-1);
             else
                 {
       System.out.println(result);
             result="";
             }
        }    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    }
}
 
     
    