Ctrl K

Word Growth Chain

Medium
You are given a list words of lowercase English words. Word B *grows* from word A if inserting exactly one letter anywhere in A, without reordering the other letters, gives B. For example, cat grows into coat, but tac does not. A *growth chain* is a sequence of words from the list where each word grows from the one before it. A single word on its own is a chain of length 1. Return the length of the longest growth chain you can build from the list.
Example 1
words=["to", "t", "tor", "stor", "story", "top"] → 5

t → to → tor → stor → story: each step inserts exactly one letter. The branch to → top is shorter.

Example 2
words=["ab", "bca"] → 1

bca cannot be made from ab by one insertion without swapping a and b, so the best chain is a single word.

Example 3
words=["k", "ok", "kok", "oko"] → 3

k → ok → kok (or ok → oko) gives three words.

Constraints
  • 1 ≤ len(words) ≤ 1000
  • 1 ≤ len(words[i]) ≤ 30
  • words consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Subsequence of One Word Only MediumWords Hidden in a Text MediumSpells Without Neighbors MediumErasable Marked Letters MediumAnagram Buckets Medium
loading Python…
Input
words=["to", "t", "tor", "stor", "story", "top"]
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 83 tests on the server, hidden ones included.