mediumArrayString 0 views

Longest Common Prefix Between Adjacent Strings After Removals

You are given an array of strings words.

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

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.

Longest Common Prefix Between Adjacent Strings After Removals diagram

Example 1

Input: words = ["jump","run","run","jump","run"]

Output: [3,0,0,3,3]

Example 2

Input: words = ["dog","racer","car"]

Output: [0,0,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.

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.