4

How Do You Find the Longest Common Subsequence of Two Strings in Java?

 1 year ago
source link: https://hackernoon.com/how-do-you-find-the-longest-common-subsequence-of-two-strings-in-java
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How Do You Find the Longest Common Subsequence of Two Strings in Java?

Notifications
Over 100,000 votes have been casted for this year’s Noonies Nominees. Vote for your favorite now!
Last Monday at 2:18 PM
Happy Weekend, enjoy these top stories from this week, Genetic Load, The First NFT, and more 💚
Last Saturday at 6:00 PM
There have been over 90,000 votes cast in this year's Noonies! Vote for your favorite tech stars today
09/26/2022
Happy Weekend, enjoy these top stories from this week, Land-Tax, Snippets, and more 💚
09/24/2022
Vote now on HackerNoon weekly polls!
09/22/2022
#Noonies voting closes on November 15th! Vote now for your favorite tech-pioneers!
09/20/2022
Start off your week the right way! Enjoy the top stories put out by the beautiful people of HackerNoon!
09/20/2022
Don't miss out on the daily top trending stories on HackerNoon! Subscribe to TechBeat to see what people are currently interested about!
09/20/2022
Do you know #Noonies voters can also win a free .Tech Domain? For details, check your email after casting the vote.
09/12/2022
The Sandbox is back with #EnterTheMetaverse Writing Contest. Win from $18,000 prize pool!
09/12/2022
HackerNoon now publishes sci-fi! Read some of our science fiction stories today and submit your own!
09/05/2022
$200k+ in Committed Writer Payouts for HackerNoon Writing Contests. Enter to Win Monthly Prizes!
08/29/2022
The Blockchain Writing Contest 2022: Round 5 Results Announced!
08/18/2022
mParticle and HackerNoon brings you the Growth Marketing Writing Contest starting from September!
08/08/2022
Enter the #DeFi Writing Contest by Sora and Hackernoon. $30k in prizes.
07/04/2022
Discover Trending Tech Companies via Tech Company Brief
07/04/2022
see 9 more
How Do You Find the Longest Common Subsequence of Two Strings in Java? by@ishitajuneja

How Do You Find the Longest Common Subsequence of Two Strings in Java?

A simple approach to find the longest common subsequence of a string is followed by a simple algorithm. Dynamic programming is used to solve the problem using a simple approach. The process includes the following steps: – create a table with dimensions (m+1*(n+1) and fill the other cells of your created table. In case the characters present in the corresponding row and column are the same, you will have to add 1 to the current cell which is diagonal to the element. Also, the last column and row will determine the length of the table.
image
Audio Presented by
Lisk-icon
Speed:
Read by:
Your browser does not support theaudio element.

Ishita Juneja

A professionally trained Tech Expert.

Strings are nothing but a combination of characters, and working on strings is a common part of a programmer’s life.

Questions related to strings are often asked in interviews. Whether it is about finding the next permutation or finding the longest common subsequence, you will find one or more questions related to strings in an interview.

Here, we are going to discuss one common problem i.e, printing the longest common subsequence. 

One thing that you have to note here is that there is a difference when it comes to finding a common subsequence and printing longest common subsequence

The Problem Statement

You will be given two strings and you will have to determine the longest common subsequence between the given strings. Also, keep in mind that the strings should be in the same order only.

A subsequence of a string is defined as the sequence of characters that you can derive from the string without changing any element of the string. The order of the other remaining string should be the same.

To understand the problem, consider the following example:

You are given two strings: string 1: AECFDEF and String 2: BACGDGBF

Now, the output of the program will be ACDF because, among all the other subsequences, it is the largest one.

So, now you may have an idea of what a subsequence of a string is. Let’s proceed with how you can find the longest common subsequence in two strings.

How To Find The Longest Common Subsequence In Two Strings

To find the longest common subsequence, you can use two methods. They are -

  • Simple approach
  • Dynamic Programming

Simple Approach

To find the longest common subsequence, a simple approach is followed, you need to check for each subsequence of string 1 and ascertain whether or not it is a subsequence of string 2.

Consider that the lengths of S1 and S2 subsequences are m and n respectively.

Here, the function will accept two sequences as its parameters and will use two iterators to traverse through the sequence.

Also, in this algorithm, things can get tricky when any of the lengths are not valid.

You will then have to check the present element in both iterators and if they are equal, you need to include it in the largest common subsequence. Also, recursively call the previous element in each string.

However, in case the iterators are not equal, you can not obtain LCS and include any present element in it. 

Complexity Analysis

- Time Complexity: The time complexity of this method is O(2^(m+n)). Here, m and n are the lengths of the given strings.

- Space Complexity: Now that the algorithm uses no additional space, the space complexity of this method is calculated as O(1).

Dynamic Programming

While using a simple approach, there are times when the same subproblem is analyzed multiple times or when some problems may get overlapped. In that case, an effective solution is used. 

To overcome all the drawbacks of the simple approach, dynamic programming is used to find the longest common subsequence.

The process includes the following steps:

To begin with, you need to first create a table having dimensions (m+1)*(n+1). Here, m and n are the sizes of the given strings. Now, you will have to set 0 in the first column and row of the table.

After this, you need to follow the following steps to fill the other cells of your created table:

In case the characters present in the corresponding row and column are the same, you will have to add 1 to the current cell which is diagonal to the element. Also, point an arrow toward the diagonal cell.

In case the characters are not the same, we need to fill the present cell with the same values as the previous one. The arrow must point to a cell that has a maximum value. Moreover, when values in both cells become equal, you can point the arrow to any one of them.

You will have to repeat step 2 until the table is filled completely.

Now, the last column and row will determine the length of the required common subsequence.

Moreover, to find the longest common subsequence, you need to follow the arrow’s direction from the ending element. All the elements adjacent to the brackets will create the required subsequence.

One thing that you have to remember about these methods is that these methods will help you find the length of the subsequence. You will not be able to print the actual value of the subsequence. So, if you wish to print the value, check out what you need to do for printing the longest common subsequence.

How To Print The Longest Common Subsequence

For printing the longest common subsequence, you need to carry out a different algorithm. Here are all the steps involved in the process.

To begin with, you will have to create L[m+1][n+1].

Here, L[m][n] will contain the length of the longest common subsequence. You will have to character array of the length the same as that of LCS+1.

You will have to then iterate through the 2D array beginning from L[m][n]. For each cell of L[i][j], you need to perform the following steps.

In case the characters adjoining to L[i][j] are the same, you will have to include that character as part of the longest common subsequence.

Otherwise, you will have to compare the value of L[i][j-1] and L[i-1][j]. You will then have to go in the direction of the larger value.

Conclusion

The subsequence of a string is simply a sequence of characters present in the main array. When it comes to finding the longest common subsequence, you find it either using dynamic programming or a simple recursive method.

However, when it comes to printing the longest common subsequence, the algorithm that you need to follow is different. We have explained to you the complete approach that you have to follow. 

4
reaction animationreaction animationreaction animation
reaction animationreaction animationreaction animation
reaction animationreaction animationreaction animation
reaction animationreaction animationreaction animation
by Ishita Juneja @ishitajuneja.A professionally trained Tech Expert.
Read my stories
L O A D I N G
. . . comments & more!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK