• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Solve the Longest Common Subsequence Problem

laptop infographic online business 6087062

Introduction

The Longest Common Subsequence (LCS) problem is a classic computer science problem used in fields like bioinformatics, file comparison, and version control systems. It involves finding the longest sequence of characters that appear left-to-right (but not necessarily contiguously) in both input strings.

For example, given the strings "ACDBE" and "ABCDE", the LCS is "ACDE".

Problem Definition

The problem is about finding the length (optionally, the content) of the longest subsequence present in both of two sequences. That is a sequence that appears in the same relative order, but not necessarily contiguously.

This is different from substrings, which require characters to be continuous.

Dynamic Programming Approach

The LCS problem can be efficiently solved using dynamic programming. We use a 2D array to store solutions to subproblems. The idea is to build a table where each cell (i, j) contains the length of the LCS of the first i characters of one string and the first j characters of the other string.

If characters at position i-1 and j-1 are equal, then
dp[i][j] = dp[i-1][j-1] + 1
Otherwise,
dp[i][j] = max(dp[i-1][j], dp[i][j-1])

Java Implementation

Here’s a Java program that calculates the LCS length and also reconstructs the LCS string.

public class LCSProblem {

    public static String longestCommonSubseq(String input1, String input2) {
        int m = input1.length();
        int n = input2.length();
        int[][] dbArr = new int[m + 1][n + 1];

        // Build the DP table
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (input1.charAt(i - 1) == input2.charAt(j - 1)) {
                    dbArr[i][j] = dbArr[i - 1][j - 1] + 1;
                } else {
                    dbArr[i][j] = Math.max(dbArr[i - 1][j], dbArr[i][j - 1]);
                }
            }
        }

        // Reconstruct the LCS from the DP table
        StringBuilder lcsString = new StringBuilder();
        int i = m, j = n;

        while (i > 0 && j > 0) {
            if (input1.charAt(i - 1) == input2.charAt(j - 1)) {
                lcsString.append(input1.charAt(i - 1));
                i--;
                j--;
            } else if (dbArr[i - 1][j] > dbArr[i][j - 1]) {
                i--;
            } else {
                j--;
            }
        }

        return lcsString.reverse().toString();
    }

    public static void main(String[] args) {
        String s1 = "ACDBE";
        String s2 = "ABCDE";
        String result = longestCommonSubseq(s1, s2);
        System.out.println("LCS: " + result);
    }
}

Output

For the input strings "ACDBE" and "ABCDE", the program will output:

makefileCopiaModificaLCS: ACDE

Time and Space Complexity

Time Complexity: O(m * n), where m and n are lengths of the two strings.
Space Complexity: O(m * n) due to the 2D array used for memoization.

If only the length of the LCS is required, and not the sequence itself, the space complexity can be optimized to O(min(m, n)).

Conclusion

The Longest Common Subsequence problem is a foundational problem in computer science that demonstrates the power of dynamic programming. It’s applicable in real-world tasks such as diff tools, merging changes in version control, and DNA sequence analysis. With efficient implementations, LCS can be used in systems requiring performance and accuracy.

You can find the example code on Github.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions