# Longest Common Subsequence (Java)

The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences).

Analysis

Let dp[i+1][j+1] be the length of the longest common subsequence of string a & b, when a[i] and b[j] are compared to each other. (0<=i<=a.length-1, 0<=j<=b.length-1)

Java Solution

```public static int getLongestCommonSubsequence(String a, String b){ int m = a.length(); int n = b.length(); int[][] dp = 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){ dp[i][j]=0; }else if(a.charAt(i-1)==b.charAt(j-1)){ dp[i][j] = 1 + dp[i-1][j-1]; }else{ dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); } } }   return dp[m][n]; }```
Category >> Algorithms
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
1. pavan on 2017-9-4

This have clear explanation of how you will come to this approach

http://www.geeksforgeeks.org/longest-common-subsequence/

2. Raj on 2017-11-20

Hi,

It will give the length of that subsequence but can you explain how to get that string ?

Regards
Raj