Longest Common Prefix Between Adjacent Strings After Removals - Problem

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

1. Remove the element at index i from the words array.

2. 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.

Input & Output

Example 1 — Basic Case
$ Input: words = ["apple", "app", "application", "banana"]
Output: [3, 3, 2, 3]
💡 Note: Remove "apple": remaining ["app", "application", "banana"], max prefix = 3 (app-application). Remove "app": remaining ["apple", "application", "banana"], max prefix = 3 (apple-application). Remove "application": remaining ["apple", "app", "banana"], max prefix = 2 (apple-app). Remove "banana": remaining ["apple", "app", "application"], max prefix = 3 (app-application).
Example 2 — No Common Prefixes
$ Input: words = ["cat", "dog", "bird"]
Output: [0, 0, 0]
💡 Note: No adjacent pairs share any common prefix characters, so all results are 0.
Example 3 — Two Elements
$ Input: words = ["test", "testing"]
Output: [0, 0]
💡 Note: Removing either element leaves only one string, so no adjacent pairs exist.

Constraints

  • 2 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 100
  • words[i] consists of lowercase English letters

Visualization

Tap to expand
Longest Common Prefix After Removals INPUT words array: [0] "apple" [1] "app" [2] "application" [3] "banana" Adjacent Pairs (Original): "apple"-"app" LCP=3 "app"-"application" LCP=3 "application"-"banana" LCP=0 Max LCP = 3 ALGORITHM STEPS 1 Pre-compute LCP Calculate LCP for each adjacent pair in array 2 Build Prefix Max prefix[i] = max LCP from pairs 0 to i-1 3 Build Suffix Max suffix[i] = max LCP from pairs i+1 to end 4 Compute Answer For each removal, combine prefix, suffix, new pair Pre-computed Arrays: LCP: [3, 3, 0] prefix: [0, 3, 3, 3] suffix: [3, 3, 0, 0] Remove i=0 ("apple"): max(suffix[1]) = 3 FINAL RESULT Each Removal Case: Remove [0] "apple": ["app","application","banana"] Max LCP = 3 (app-application) Remove [1] "app": ["apple","application","banana"] Max LCP = 3 (apple-application) Remove [2] "application": ["apple","app","banana"] Max LCP = 2 (app-banana? No, app-le) Remove [3] "banana": ["apple","app","application"] Max LCP = 3 (app pairs) OUTPUT: [3, 3, 2, 3] OK - All cases computed! Key Insight: Pre-compute all adjacent pair LCPs and build prefix/suffix max arrays for O(1) lookup. When removing index i, the new adjacent pair (i-1, i+1) may form. Combine with prefix[i-1] and suffix[i+1] to get max LCP efficiently. Total time complexity: O(n * m) where m = max word length. TutorialsPoint - Longest Common Prefix Between Adjacent Strings After Removals | Pre-computed Adjacent Pairs Approach
Asked in
Google 25 Amazon 18 Microsoft 15
23.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen