AiTechWorlds
AiTechWorlds
Visualize the LCS 2D DP table comparing two strings character by character, with synced multi-language code.
| β | G | A | C | |
|---|---|---|---|---|
| β | 0 | 0 | 0 | 0 |
| A | 0 | 0 | 0 | 0 |
| G | 0 | 0 | 0 | 0 |
| C | 0 | 0 | 0 | 0 |
| A | 0 | 0 | 0 | 0 |
| T | 0 | 0 | 0 | 0 |
def lcs(a, b):
n, m = len(a), len(b)
dp = [[0]*(m+1) for _ in range(n+1)]
for i in range(1, n+1):
for j in range(1, m+1):
if a[i-1] == b[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[n][m]Longest Common Subsequence finds the longest sequence appearing in both strings in order (not necessarily contiguous), filling a 2D table by comparing characters.
| Time | O(n Γ m) |
| Space | O(n Γ m) |
The Longest Common Subsequence (LCS) Visualizer animates the 2D dynamic programming solution that finds the longest sequence appearing in both strings in order. When characters match, dp[i][j] = dp[i-1][j-1] + 1; otherwise it is the max of the cell above or to the left. It runs in O(n Γ m) time and space.
Press Play
Watch the table fill as the two strings are compared character by character.
Step through
Use Step Forward/Back or the timeline to follow each match/mismatch decision.
Read the code
View the LCS DP in C++, Java, Python, or JavaScript and copy it.
100% Private β No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.