Preset:
1x

Longest Common Subsequence (LCS)

Step 1 of 6

Find the longest sequence of characters that appears in both strings in the same relative order (not necessarily contiguous). A fundamental DP problem used in diff tools, bioinformatics, and version control.

The Problem

Given two strings s1 and s2, find the longest subsequence present in both. A subsequence keeps the original order but can skip characters. For example: s1 = "SUNDAY", s2 = "SATURDAY" → LCS = "SDAY" (length 4).

S
U
N
D
A
Y
S
A
T
U
R
D
A
Y

Green = shared subsequence "SDAY"