Longest Chain of Single-Letter Insertions

Quick Overview

Longest Chain of Single-Letter Insertions. Choose a chain of words from words. Each next word must result from inserting exactly one letter into the preceding word without changing the order of existing letters.

Longest Chain of Single-Letter Insertions

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Choose a chain of words from `words`. Each next word must result from inserting exactly one letter into the preceding word without changing the order of existing letters. A lone word is a chain of length 1. Return the greatest chain length. ```cpp class Solution { public: int longestStrChain(vector<string>& words); }; ``` ### Examples **Example 1:** ```text Input: words = ["a","ac","bac","bacc","zz"] Output: 4 Explanation: a -> ac -> bac -> bacc adds one letter at each step. ``` **Example 2:** ```text Input: words = ["ab","ba","xyz"] Output: 1 Explanation: Equal-length words cannot be consecutive in a chain, and deleting one character from xyz yields neither ab nor ba. ``` ### Constraints - There are 1 to 1000 input words. - Each word has length 1 to 16. - Words use lowercase English letters only. ### Contract considerations Use only words from the given list. Return the chain length, not one selected chain. ```hint Read the boundary condition Must every word in a proposed chain occur in the input, and can equal-length words form a step? ``` ### Boundary reminders Use only words from the given list. Return the chain length, not one selected chain.

Overview: Longest Chain of Single-Letter Insertions. Choose a chain of words from words. Each next word must result from inserting exactly one letter into the preceding word without changing the order of existing letters.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Choose a chain of words from words. Each next word must result from inserting exactly one letter into the preceding word without changing the order of existing letters. A lone word is a chain of length 1. Return the greatest chain length.

class Solution {
public:
    int longestStrChain(vector<string>& words);
};

Examples

Example 1:

Input: words = ["a","ac","bac","bacc","zz"]
Output: 4
Explanation: a -> ac -> bac -> bacc adds one letter at each step.

Example 2:

Input: words = ["ab","ba","xyz"]
Output: 1
Explanation: Equal-length words cannot be consecutive in a chain, and deleting one character from xyz yields neither ab nor ba.

Constraints

  • There are 1 to 1000 input words.
  • Each word has length 1 to 16.
  • Words use lowercase English letters only.

Contract considerations

Use only words from the given list. Return the chain length, not one selected chain.

Boundary reminders

Use only words from the given list. Return the chain length, not one selected chain.