Finding the Longest Common Subsequence (LCS) of Two Strings

Mastering The LCS Problem in JavaScript

Arnold Abraham
4 min readAug 24, 2023

JavaScript Challenges: Beginner to Master — Task #52

Create a function that finds the two strings' longest common subsequence (LCS). The function should take two strings as input and return the LCS. The LCS is the longest sequence of characters that appear in both strings in the same order.

LCS — Longest Common Subsequence

This is a classic problem in computer science and dynamic programming.
Given two sequences (or strings), the goal is to 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 consecutively.

Step-by-Step Solution of Task #52

Step 1: Define the LCS Function

Begin by defining our final function, called longestCommonSubsequence that accepts two string arguments: s1 and s2. When complete, it will return the LCS of the input strings.

function longestCommonSubsequence(s1, s2) { ... }

Step 2: Initialize String…

--

--

Arnold Abraham

JavaScript, TypeScript and C#/.NET Tutorials/News/Best Practices by a German Software Engineer - Fun helps you to learn on the fly --> arnoldcode.com