Launch your tech mastery with us—your coding journey starts now!
Course Content
Data Structure

Longest Common Subsequence 

Definition

Given two strings, find the length of the longest subsequence present in both of them. A subsequence is a sequence that appears in the same relative order, but not necessarily contiguously.

  • Example: “ACE” is a subsequence of “ABCDE”.

How it Works

We compare the two strings character by character.

  1. Match: If characters match (e.g., ‘A’ == ‘A’), we add 1 to the result and move both pointers forward.
  2. No Match: If they don’t match, we branch into two possibilities:
  • Skip a character from string A.
  • Skip a character from string B.
  • Take the maximum of these two options.

Code Example (Java)

public class LCS_DP {

    public static int lcs(String X, String Y) {
        int m = X.length();
        int n = Y.length();

        // Create DP table
        int[][] dp = new int[m + 1][n + 1];

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {

                if (X.charAt(i - 1) == Y.charAt(j - 1)) {
                    dp[i][j] = 1 + dp[i - 1][j - 1]; // Match found
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); // No match
                }
            }
        }

        return dp[m][n];
    }

    public static void main(String[] args) {
        String s1 = "AGGTAB";
        String s2 = "GXTXAYB";

        System.out.println("Length of LCS is " + lcs(s1, s2)); // Output: 4
    }
}

Complexity Analysis

  • Time Complexity: O(M times N) where M and N are string lengths.
  • Space Complexity: O(M times N) for the table.

Use Cases

  • Diff Tools: Comparing two versions of a file (like Git).
  • DNA Sequencing: Finding similarities between gene strands.