Skip to content
AI360Xpert
Beta
Difficulty: Medium2-D Dynamic Programming

Longest Common Subsequence

Problem in Plain English

Given two strings, find the length of the longest sequence of characters that appears in both strings in the same relative order. Characters may be skipped but never reordered.

Problem Statement

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

A subsequence is formed by deleting zero or more characters from a string without changing the relative order of the rest. For text1 = "abcde" and text2 = "ace", the answer is 3 because "ace" appears in both in the same order.

Constraints

  • 1 <= text1.length, text2.length <= 1000
  • text1 and text2 consist of only lowercase English letters.

Examples

Example 1

Input
text1 = "abcde", text2 = "ace"
Output
3

Choose a, c, and e at positions 0, 2, and 4 of abcde; ace supplies the same three characters in order. Length 3 reaches the shorter string length, so no longer answer is possible. The gaps show that this is a subsequence, not a substring.

Example 2

Input
text1 = "abc", text2 = "abc"
Output
3

All three characters match in order without deleting anything. The answer cannot exceed either input length, so the whole string abc is optimal with length 3.

Example 3

Input
text1 = "abc", text2 = "def"
Output
0

There is no shared character at all. Only the empty subsequence appears in both strings, so its length 0 is the answer even though neither input is empty.

Example 4

Input
text1 = "abc", text2 = "bac"
Output
2

Either ac or bc is shared in order. A length-3 answer would have to use every character of both strings, but their first two characters have opposite order, so 3 is impossible.

Example 5

Input
text1 = "a", text2 = "aaaa"
Output
1

The single a can be used only once. Repeated matches in the second string do not create extra characters in the first.

Intuition

A subsequence preserves order but allows gaps. In abcde and ace, skip b and d while matching a, c, and e, giving length 3. For two remaining suffixes, matching heads can contribute one character and advance both positions. Different heads cannot both be the first matched pair, so try discarding one head or the other and keep the better length. Repeated suffix pairs motivate a table; the bottom-up version stores prefix lengths instead.

Approaches

Brute Force Recursion

Solution Details

Reveal Brute Force Recursion: intuition, complexity, and code

Hints

Hint 1
Compare the strings from one end and ask what must be true when the current characters match.
Hint 2
When current characters differ, at least one must be skipped before a matched pair can start; compare both skips rather than committing greedily.
Hint 3
Name the state after the two positions already consumed, then fill a grid where each cell reuses the cells above, left, and diagonally above-left.

Edge Cases

  • No shared characters, as in text1 = "abc" with text2 = "def", so the answer is 0.
  • One string is entirely a subsequence of the other, so the answer equals the shorter length.
  • Both strings share long repeated characters, which the recurrence handles without any special casing.
  • One character against a repeated run, such as a and aaaa, still contributes only one match.
  • Different order can shorten the answer despite equal character counts: abc and bac have length 2, not 3.

Common Mistakes and Interview Tips

  • Treating the problem as longest common substring and requiring contiguity, which undercounts answers like "ace" inside "abcde".
  • Using a greedy first-match scan, which can consume a character early and block a longer subsequence later.
  • Sizing the table as m by n instead of m + 1 by n + 1, which removes the zero base row and breaks the recurrence.
  • At a recursive match, advance both indices. Example 1 goes from (0,0) to (1,1), not (1,0).
  • Resetting mismatches to zero solves a substring problem; subsequences retain the better top or left value.
  • Treating the bottom-up prefix table as a suffix table and filling it backwards reads uncomputed dependencies.
  • In Python, creating rows with [[0] * columns] * rows aliases storage; each row must be allocated independently.
  • Interview tip: length computation and actual sequence reconstruction are different outputs. State the requested output and explain the recurrence before coding.

Key Takeaway

When the answer depends on two prefixes at once, index the state by both positions and let each cell combine its top, left, and diagonal neighbours.