Longest String Chain
A medium Dynamic Programming problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Dynamic Programming
- Sheets
- 1
- Core for
- 9 roles
- Platform
- LeetCode
The problem
Given a list of words, find the length of the longest string chain where each word is a predecessor of the next. A word A is a predecessor of word B if you can insert exactly one letter into A to make it equal to B.
Example 1
- Input
- words = ["a","b","ba","bca","bda","bdca"]
- Output
- 4
- Why
- The longest chain is "a" -> "ba" -> "bda" -> "bdca", length 4.
Example 2
- Input
- words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
- Output
- 5
- Why
- The chain "xb" -> "xbc" -> "cxbc" -> "pcxbc" -> "pcxbcf" has length 5.
Example 3
- Input
- words = ["abcd","dbqca"]
- Output
- 1
- Why
- "abcd" cannot be a predecessor of "dbqca" (not just one insertion), so the longest chain is length 1.
Constraints
- 1 <= words.length <= 1000
- 1 <= words[i].length <= 16
- words[i] consists of lowercase English letters
How to think about it
Updated 2026-09-09Instead of searching forward by trying to insert all 26 letters at every position of word A, search backward from word B by deleting one character at each of its L positions. If that smaller word exists in your dictionary, it extends whatever chain ended there.
Approaches, worst first
Graph construction with DFS
time O(n^2 * L) · space O(n^2)
Build a directed acyclic graph by comparing all pairs of words (u, v) where len(v) == len(u) + 1. Then run DFS to find the longest path.
Length-sorted map with single-character deletionsWrite this one
time O(n log n + n * L^2) · space O(n * L)
Sort words by length ascending. Store chain lengths in a hash map. For each word of length L, generate all L predecessor words by removing one character at a time. The word's chain length is 1 + max(map[predecessor]).
Where people lose marks · 3
- Processing words out of length order. Shorter words must be completely solved before longer words attempt to query their chain lengths.
- Generating successors (inserting letters) instead of predecessors (deleting letters). Successor generation tests 26 * (L + 1) variations rather than just L deletions.
- A single word always forms a chain of length 1, so the initial answer must be at least 1.
The theory behind it
Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems
What Dynamic Programming is
Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.
When to reach for it
Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.
How the pattern works
Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.
What each operation costs
| Operation | Time |
|---|---|
| fill dynamic programming table of n states | O(n) |
| solve two-dimensional grid of m by n states | O(m * n) |
| space-optimized state transition keeping one row | O(n) |
What usually goes wrong with Dynamic Programming
- Filling a bottom-up table in an order where the current cell needs values that have not been computed yet, reading uninitialized zeros.
- Failing to initialize base cases properly, such as filling a minimization table with zeros instead of infinity, which traps the answer at zero.
- Overwriting values in a 1D space-optimized knapsack array by scanning in the wrong direction, allowing the same item to be chosen multiple times.
Which roles need this problem
Dynamic Programming is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 5 more roles, including Game Developer, Cryptography Engineer, Performance Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Dynamic Programming problems
Problem set and role mapping as of .