Computer Science, asked by bvani7232, 8 days ago

write a java program to find the longest substring appears at both ends of a given string​

Answers

Answered by puspendrarenubhardwa
0

Answer:

this is your answer and please click the brain list

Attachments:
Answered by ravilaccs
0

Answer:

The Program is constructed with the given statement.

Explanation:

Problem construct

  • findLongestSubString(StringBuilder) : StringBuilder
  • Should accept StringBuilder as input and return StringBuilder
  • Should find the longest substring that appears at both ends of a given StringBuilder
  • Should return "Longest substring is not present in the given StringBuilder" if longest substring does not exist
  • -Should return "Give proper input" if input is empty StringBuilder

Sample Input:

playerplay

Sample Output:

Play

Sample Input:

playerplays

Sample output:

Longest substring is not present in the given StringBuilder

Program

package substring;

public class Substring {

static String findLongestSubString(String string) {

       int size = string.length();

       int LCSRe[][] = new int[size + 1][size + 1];

       String results = "";

       int length = 0;  

      int i, index = 0;

       for (i = 1; i <= size; i++) {

           for (int j = i + 1; j <= size; j++) {

               if (string.charAt(i - 1) == string.charAt(j - 1)

                       && LCSRe[i - 1][j - 1] < (j - i)) {

                   LCSRe[i][j] = LCSRe[i - 1][j - 1] + 1;                  

                   if (LCSRe[i][j] > length) {

                       length = LCSRe[i][j];

                       index = Math.max(i, index);

                   }

               } else {

                   LCSRe[i][j] = 0;

               }

           }

       }

       if (length > 0) {

           for (i = index - length + 1; i <= index; i++) {

               results += string.charAt(i - 1);

           }

       }

       else{

        System.out.println("Longest substring is not present in the given StringBuilder");

       }

       return results;

   }  

   public static void main(String[] args) {

      String str = "playerplay";

       System.out.println(findLongestSubString(str));

   }    

}

Reference Link

  • https://brainly.in/question/16088708
  • https://brainly.in/question/9753572
  • https://brainly.in/question/33637880
Similar questions