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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code