LeetCode Problem

How to Solve Longest String Chain

This problem is best approached by scanning words in increasing length and using a hash map to track the maximum chain ending at each word. Deleting one character at a time from the current word simulates predecessor links, letting us update chain lengths efficiently. GhostInterview guides you to implement this pattern correctly and avoid common pitfalls like misordering or redundant recomputation.

GhostInterview Help

Need help with Longest String Chain without spending extra time grinding it?

GhostInterview can read Longest String Chain from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1048Array scanning plus hash lookupReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem is best approached by scanning words in increasing length and using a hash map to track the maximum chain ending at each word. Deleting one character at a time from the current word simulates predecessor links, letting us update chain lengths efficiently. GhostInterview guides you to implement this pattern correctly and avoid common pitfalls like misordering or redundant recomputation.

Problem Statement

You are given an array of words where each word consists of lowercase English letters. A word wordA is considered a predecessor of wordB if you can insert exactly one letter anywhere in wordA without changing the relative order of the other characters to produce wordB. The goal is to determine the length of the longest possible word chain from the given array.

A word chain is a sequence of words [word1, word2, ..., wordk] where k >= 1, and each word is a predecessor of the next. A single word counts as a valid chain of length 1. For example, in words = ["a","b","ba","bca","bda","bdca"], one longest chain is ["a","ba","bda","bdca"] with length 4.

Examples

Example 1

Input: words = ["a","b","ba","bca","bda","bdca"]

Output: 4

One of the longest word chains is ["a","ba","bda","bdca"].

Example 2

Input: words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]

Output: 5

All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].

Example 3

Input: words = ["abcd","dbqca"]

Output: 1

The trivial word chain ["abcd"] is one of the longest word chains. ["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.

Constraints

  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 16
  • words[i] only consists of lowercase English letters.

Solution Approach

Sort Words by Length

Start by sorting the words array by word length. This ensures that when scanning words, all potential predecessors of a word have already been processed and stored in the hash map, which is crucial for correctly computing the longest chain in one pass.

Use Hash Map to Track Chains

Maintain a hash map where each word maps to the length of the longest chain ending with that word. For each word, try deleting one character at every position to form a predecessor candidate. If the candidate exists in the hash map, update the current word's chain length to be one more than the predecessor's chain.

Compute Maximum Chain

After processing all words, the maximum value in the hash map represents the length of the longest string chain. This avoids redundant recomputation and leverages the array scanning plus hash lookup pattern efficiently.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(N * L^2), where N is the number of words and L is the maximum word length, since each word can generate up to L predecessor candidates. Space complexity is O(N * L) for storing words in the hash map and their chain lengths.

What Interviewers Usually Probe

  • They may ask why we delete characters instead of inserting them during chain building.
  • Expect discussion on time-space trade-offs using hash maps versus dynamic programming arrays.
  • Clarify handling of words with the same length but different character order to avoid invalid chains.

Common Pitfalls or Variants

Common pitfalls

  • Failing to sort words by length, which can lead to incorrect predecessor evaluation.
  • Using insertion instead of deletion, which complicates finding valid predecessor words.
  • Overwriting chain lengths without checking all predecessor candidates, missing longer chains.

Follow-up variants

  • Find all longest chains instead of just the length, returning sequences of words.
  • Modify constraints to allow uppercase letters and non-English alphabets, requiring extended hash handling.
  • Count the number of distinct longest chains instead of maximum length.

How GhostInterview Helps

  • GhostInterview highlights the array scanning plus hash lookup pattern, guiding correct predecessor evaluation.
  • It identifies subtle failure modes like character order misalignment and redundant recomputation.
  • Provides step-by-step hints for implementing the chain calculation efficiently using deletion-based mapping.

Topic Pages

FAQ

What is the main pattern to solve Longest String Chain?

The key pattern is array scanning plus hash lookup, using deletion of characters to map predecessors and compute the maximum chain efficiently.

Can I insert characters instead of deleting to find chains?

While insertion is theoretically equivalent, deleting one character from the current word simplifies hash lookup and avoids scanning unnecessary combinations.

Do words with the same length affect the solution?

Yes, words of the same length should be processed individually, as they cannot be predecessors of each other, ensuring valid chain construction.

What is the time complexity for the solution?

Time complexity is O(N * L^2), where N is the number of words and L is the maximum word length, due to generating all possible predecessors per word.

Is sorting necessary before computing chains?

Yes, sorting words by length guarantees that all potential predecessors are already in the hash map when processing a word, avoiding incorrect chain lengths.

GhostInterview Solver

Need direct help with Longest String Chain instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Longest String Chain from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.