Skip to main content
Back to problems
Leetcode
Medium
Strings
Arrays
Hash Maps
Google
Meta
Longest Common Prefix Between Adjacent Strings After Removals

For each index removal query, determine the maximum common prefix length among all adjacent strings in the remaining array.

Acceptance 0%
Problem Statement

Problem

You are given an array of strings words. For each query, one index is removed from the array, and the remaining strings keep their original relative order.

For the array that remains after the removal, consider every pair of adjacent strings. For each adjacent pair, compute the length of their longest common prefix (LCP). Your task is to return the maximum LCP length among all adjacent pairs in the remaining array.

If fewer than two strings remain after a removal, the answer for that query is 0.

Goal

Process all queries and report the maximum adjacent-prefix value after each removal.

Notes

  • The longest common prefix of two strings is the longest string that is a prefix of both.
  • Only adjacent pairs in the current array matter after each removal.
  • Removing one element can only affect the pairs around that element; other adjacent pairs remain unchanged.

Input Format

  • An array words of strings.
  • A list of removal queries, where each query specifies an index in words to remove.

For each query, consider the array formed by deleting that one element and keeping the rest in order.

Output Format

  • Return an array of integers.
  • The i-th value is the maximum LCP length among all adjacent pairs after applying the i-th removal query.

Constraints

  • 1 <= words.length
  • Each query removes exactly one valid index.
  • Strings contain lowercase English letters.
  • The answer for a query is 0 if the remaining array has fewer than 2 strings.

Exact limits are not provided here; assume the intended solution should be efficient across many queries.

Examples
Sample cases returned by the problem API.

Example 1

Input

words = ["flower", "flow", "flight", "flute"], queries = [1, 2]

Output

[2, 4]

Explanation

  • Remove index 1 ("flow"): remaining words are ["flower", "flight", "flute"]. Adjacent LCPs are fl = 2 and fl = 2, so the maximum is 2.
  • Remove index 2 ("flight"): remaining words are ["flower", "flow", "flute"]. Adjacent LCPs are flow vs flower = 4 and flow vs flute = 2, so the maximum is 4.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.