Mid/SeniorArrayString

Longest Common Prefix Between Adjacent Strings After Removals

You are given an array of strings words. For each index i in the range [0, words.length - 1], perform the following steps:

  • Remove the element at index i from the words array.
  • Compute the length of the longest common prefix among all adjacent pairs in the modified array.

Return an array answer, where answer[i] is the length of the longest common prefix between the adjacent pairs after removing the element at index i.

If no adjacent pairs remain or if none share a common prefix, then answer[i] should be 0.

Example 1
Inputwords = ["jump","run","run","jump","run"]
Output[3,0,0,3,3]
After each removal, the longest common prefix lengths among adjacent pairs are 3, 0, 0, 3, and 3 respectively.
Example 2
Inputwords = ["dog","racer","car"]
Output[0,0,0]
Removing any index results in an answer of 0.

Constraints

  • 1 <= words.length <= 10^5
  • 1 <= words[i].length <= 10^4
  • words[i] consists of lowercase English letters.
  • The sum of words[i].length is smaller than or equal 10^5.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate