2Brute2Best
🔥0

Longest String Chain

Medium·LeetCode #1048 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You get a list of words made of lowercase letters. Word A is a predecessor of word B when you can insert exactly one letter into A, at any position, and get B.

A chain is a list of words where each word is a predecessor of the next one. A single word is a chain of length 1. Return the length of the longest chain you can make from the list.

Input

words, a list of words. The list is not sorted.

Output

The length of the longest chain of words.

Example 1
Input: words = ["a", "b", "ba", "bca", "bda", "bdca"]
Output: 4

One longest chain is a → ba → bda → bdca. Each word is the one before it with one letter inserted.

Example 2
Input: words = ["xbc", "pcxbcf", "xb", "cxbc", "pcxbc"]
Output: 5

The chain xb → xbc → cxbc → pcxbc → pcxbcf adds one letter at each step.

Example 3
Input: words = ["abcd", "dbqca"]
Output: 1

Neither word is a predecessor of the other, so the longest chain has one word.

Constraints
  • • 1 ≤ words.length ≤ 1000
  • • 1 ≤ words[i].length ≤ 16
  • • words[i] consists only of lowercase English letters.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“insert exactly one letter”
→ The next word is one letter longer. Sorting by length puts the words in an order where predecessors come first.
“longest chain”
→ We want the length of the longest chain. Keep the best length for each word, and take the maximum at the end.
“at most 16 letters”
→ A word has at most 16 deletions, so looking up all of its predecessors is cheap.
03

Approach Complexity Ladder

Brute Force — try every word before each wordO(2^N · N · L)O(N)

Recursion: the longest chain ending at a word tries every word that can come before it, with no memory

Better — sort by length, then compare every earlier wordO(N^2 · L)O(N)

Sort by length; best[i] = 1 + best[j] for each earlier word j that is a predecessor of words[i]

Optimal — delete one letter, look it up in a hash mapO(N · L^2)O(N · L)

Sort by length; for each word, try its deletions in a hash map of best chain lengths

Brute Force Intuition

Let chainEnd(w) be the longest chain that ends with word w. It is 1 plus the largest chainEnd over the words that are predecessors of w, or just 1 if there are none. Try every word in the list, and do not remember any answer. The answer is the largest chainEnd over all words.

Recognition rule for this sub-pattern:

Same idea with a custom "can follow" rule; keep parent pointers to rebuild the chain.
Remember this · DP — Longest Increasing Subsequence
Trigger
Longest chain where each item must be bigger/fit inside the previous one (numbers, pairs, envelopes, words).
Think
Does each element extend the best chain ending at some smaller earlier element?
Key question
Sort first? Then what does "increasing" mean for these items?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Longest Increasing Subsequence translate into production architectures and low-level memory mechanics.