-2

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. */
    }
}
  • 2
    This isn't really the type of questions that we answer here since your code does not have any problem. – Yassin Hajaj Jul 23 '16 at 20:10
  • That said, you may want to use `String#contains` somewhere ;) – Yassin Hajaj Jul 23 '16 at 20:11
  • Thank you for replying. I wish you could be more specific about where to use it. – govind shah Jul 23 '16 at 20:26
  • 1
    maybe code review (http://codereview.stackexchange.com) is a more appropriate place, but... your question is kind of unclear. reduce the "compile time"? that doesn't make sense. perhaps you actually meant something else. – trooper Jul 23 '16 at 20:27
  • The execution time is to be reduced. Currently taking a lot of time to execute. – govind shah Jul 23 '16 at 20:28
  • @YassinHajaj It does: It's too slow. While CR might be a good fit for this problem, SO is fine, too. Otherwise we'd have no `optimization` tag here. – maaartinus Jul 24 '16 at 01:17
  • 1
    Do you understand that compile time and execution time are entirely different things? Compile time is how long it takes for your code to compile. – Jon Skeet Jul 24 '16 at 09:15

1 Answers1

0

I assume you want to reduce execution time, as opposed to compile time. It is always best to avoid guessing, and rather determine exactly how the time is spent. This is a good example. If you do have a guess, this will prove it, or disprove it by showing you what the real problem is.

I have a guess (and it's only a guess). You have a three-level nested loop, and inside the innermost loop I see a bunch of things that look suspicious.

The biggest one is new Solution(). That hits the memory manager, which can be very costly, not only to make the objects, but to clean them up. Maybe you could move that out of the inner loop.

After that comes String temp=sub1+sub2;, which also hits the memory manager to make a new string. Maybe you could make use the the string builder.

After that comes isPalindrome. Whether that is efficient or not, I don't know.

Finally, your code needs much more disciplined indentation. That alone can cause all kinds of bugs due to not being able to follow what you're doing.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135