Answer to Question #259393 in Java | JSP | JSF for ...

Question #259393

+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







1
Expert's answer
2021-10-31T12:01:46-0400


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));
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS