Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please adjust the following java method so that it will print out the longest co

ID: 3763643 • Letter: P

Question

 Please adjust the following java method so that it will print out the longest common subsequence at the end of the call. Please show proof of output screenshot  Such as: System.out.println("The longest common subsequence is" + heres is where the lcs would be  public static String lcs(String a, String b){     int aLen = a.length();     int bLen = b.length();     if(aLen == 0 || bLen == 0){         return "";     }else if(a.charAt(aLen-1) == b.charAt(bLen-1)){         return lcs(a.substring(0,aLen-1),b.substring(0,bLen-1))             + a.charAt(aLen-1);     }else{         String x = lcs(a, b.substring(0,bLen-1));         String y = lcs(a.substring(0,aLen-1), b);         return (x.length() > y.length()) ? x : y;     } } 

Explanation / Answer

public class LongestCommonSubsequence {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       String X1 = "ABCDGH";
       String Y1 = "AEDFHR";
       System.out.println("The longest common subsequence is " + lcs(X1, Y1));

       X1 = "AGGTAB";
       Y1 = "GXTXAYB";
       System.out.println("The longest common subsequence is " + lcs(X1, Y1));
   }

   public static String lcs(String a, String b) {
       int aLen = a.length();
       int bLen = b.length();
       if (aLen == 0 || bLen == 0) {
           return "";
       } else if (a.charAt(aLen - 1) == b.charAt(bLen - 1)) {
           return lcs(a.substring(0, aLen - 1), b.substring(0, bLen - 1))
                   + a.charAt(aLen - 1);
       } else {
           String x = lcs(a, b.substring(0, bLen - 1));
           String y = lcs(a.substring(0, aLen - 1), b);
           return (x.length() > y.length()) ? x : y;
       }
   }
}

OUTPUT:

The longest common subsequence is ADH
The longest common subsequence is GTAB

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote