Question

Problem 1. Write a program in Java to find the Longest Common Subsequence (LCS) using Dynamic...

Problem 1.

Write a program in Java to find the Longest Common Subsequence (LCS) using Dynamic Programming. Your program will read two strings from keyboard and display the LCS on the screen.

Assume upper and lower case letters as same.

Sample Input (taken from keyboard):

saginaw

gain

Sample output (display on the screen):

ain

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Main {

public static void longestCommonSubsequence(String str1, String str2) {

str1 = str1.toLowerCase();

str2 = str2.toLowerCase();

int m = str1.length();

int n = str2.length();

int[][] L = new int[m+1][n+1];

for (int i=0; i<=m; i++) {

for (int j=0; j<=n; j++) {

if (i == 0 || j == 0)

L[i][j] = 0;

else if (str1.charAt(i-1) == str2.charAt(j-1))

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

else

L[i][j] = Math.max(L[i-1][j], L[i][j-1]);

}

}

int index = L[m][n];

int temp = index;

char[] lcs = new char[index+1];

lcs[index] = '\t';

int i = m, j = n;

while (i > 0 && j > 0) {

if (str1.charAt(i-1) == str2.charAt(j-1)) {

lcs[index-1] = str1.charAt(i-1);

i--;

j--;

index--;

}

else if (L[i-1][j] > L[i][j-1])

i--;

else

j--;

}

System.out.print("Longest Common Subsequence of " + str1 + " and " + str2 + " is ");

for(int k=0;k<=temp;k++)

System.out.print(lcs[k]);

}

public static void main (String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the first string: ");

String str1 = sc.nextLine();

System.out.println("Enter the second string: ");

String str2 = sc.nextLine();

longestCommonSubsequence(str1, str2);

}

}

1 import java.util.Scanner; 2 3 public class Main f public static void longestCommonSubsequence (String str1, String str2) {

NOTE: Two strings can have more than one LCS, hence the output might differ from the one you provided.

Add a comment
Know the answer?
Add Answer to:
Problem 1. Write a program in Java to find the Longest Common Subsequence (LCS) using Dynamic...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write the algorithm pseudocode for the longest common subsequence problem using dynamic programming. What is...

    1. Write the algorithm pseudocode for the longest common subsequence problem using dynamic programming. What is its running time?

  • I need this using C++. In this project, you will implement the dynamic programming-based solution to...

    I need this using C++. In this project, you will implement the dynamic programming-based solution to find the longest common subsequence (LCS) of two sequences. Your inputs will be the two sequences (as Strings) and the outputs are the longest common subsequence (printed as a String) and the final matrix (printed as a two-dimensional array) depicting the length of the longest common subsequences (as shown in the slides) for all possible subsequences of the two input sequences. The two input...

  • 3. (4 pt) Write a Python program to find the longest common prefix string amongst an...

    3. (4 pt) Write a Python program to find the longest common prefix string amongst an array of strings. You are required to write a function max- Prefix, which when called by the main program would get the array of strings as input and return a single string that would either be the longest 1 common prefix or a string ”NULL” depending on the input. (Note: The array is input from the keyboard in the main program (930)) If there...

  • Using the programming language Java: Write a function to find the longest common prefix string amongst...

    Using the programming language Java: Write a function to find the longest common prefix string amongst an array of strings.

  • Exercise #4: Some Operations on Strings Write a program that uses a C-string to store a...

    Exercise #4: Some Operations on Strings Write a program that uses a C-string to store a string entered from the keyboard (in lower case letters). The program will then provide 6 options about operations performed on the input string in the form of a menu, as shown in the following sample Input/Output. The 6 operations are given below: 1. print the string 2 reverse the string 3. print the length of the string 4. convert the lower letters to upper...

  • Must be in JAVA. Write a program that uses a Scanner to read in a String....

    Must be in JAVA. Write a program that uses a Scanner to read in a String. The program will then output a new String with all the vowels (upper and lower case) removed. See output for example output. Details Input A string composed of non-numeric characters Output The input string with the vowels removed Sample input: Welcome to Dalhousie Sample output: Dlhs nvrsty

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Create a new Java class called: House public class House Remember that Java is case-sensitive. This...

    Create a new Java class called: House public class House Remember that Java is case-sensitive. This means that the class name Diamond should start with a capital letter, and then the rest of the letters should be all lower-case. It would work if you were to use any combination of upper and lower-case letters, but that idea does not follow the assignment instructions. Write a Java application program that prints, the following house drawing to the screen: The house is...

  • The program needs to be written in C. Write a function void camelCase(char* word) where word...

    The program needs to be written in C. Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function. The program should only run when the input is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT