Longest Common Subsequence
Dynamic Programming
Asked at 12 companies
7 recent
- Apple
- Microsoft
- Visa
- Amazon
- Meta
- Bloomberg
- Optum
- Arista Networks
- ByteDance
+2 moreShow fewer
- Salesforce
- ServiceNow
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Running totals (prefix sums) · Recursion and the call stack · The base case: where the calls stop · Recursion: the base case and the call stack
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Spell check and diff: “Did you mean…?” compares words with edit distance, and diff tools align two files with longest common subsequence. Both fill a DP table.
About this problem
Break a problem into overlapping subproblems and reuse their answers.
Try it in the editor below for 20–30 minutes: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.
Examples
Full statement on LeetCode ↗Example 1
- Input
- text1 = "abcde"text2 = "ace"
- Output
- 3
Walk through it step by step(17 steps · shows the approach, so try first)
- Cell (i, j) = of the first i letters of "abcde" and the first j letters of "ace". If the letters match, it's 1 + the diagonal cell; otherwise the better of the cell above and the cell to the left.
- "a" = "a": match! 1 + diagonal (0) = 1.
- "a" ≠ "c": take the better of above (0) and left (1) = 1.
- "a" ≠ "e": take the better of above (0) and left (1) = 1.
- "b" ≠ "a": take the better of above (1) and left (0) = 1.
- "b" ≠ "c": take the better of above (1) and left (1) = 1.
- "b" ≠ "e": take the better of above (1) and left (1) = 1.
- "c" ≠ "a": take the better of above (1) and left (0) = 1.
- "c" = "c": match! 1 + diagonal (1) = 2.
- "c" ≠ "e": take the better of above (1) and left (2) = 2.
- "d" ≠ "a": take the better of above (1) and left (0) = 1.
- "d" ≠ "c": take the better of above (2) and left (1) = 2.
- "d" ≠ "e": take the better of above (2) and left (2) = 2.
- "e" ≠ "a": take the better of above (1) and left (0) = 1.
- "e" ≠ "c": take the better of above (2) and left (1) = 2.
- "e" = "e": match! 1 + diagonal (2) = 3.
- The longest common subsequence has length 3 ("ace").
Example 2
- Input
- text1 = "abc"text2 = "abc"
- Output
- 3
Example 3
- Input
- text1 = "abc"text2 = "def"
- Output
- 0
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutiondp[i][j] = of the first i letters of a and first j of b. Equal letters extend the diagonal by 1; otherwise take the better of dropping a letter from either string. One row of the table is enough.
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
prev = [0] * (len(text2) + 1)
for a in text1:
cur = [0]
for j, b in enumerate(text2):
cur.append(prev[j] + 1 if a == b else max(prev[j + 1], cur[j]))
prev = cur
return prev[-1]Reference solution, tested in Python, Java and C++.
Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.