Longest Common Prefix Between Adjacent Strings After Removals Problem
Description
LeetCode Problem 3598.
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
ifrom thewordsarray. - 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:
Input: words = ["jump","run","run","jump","run"]
Output: [3,0,0,3,3]
Explanation:
- Removing index 0:
- words becomes ["run", "run", "jump", "run"]
- Longest adjacent pair is ["run", "run"] having a common prefix "run" (length 3)
- Removing index 1:
- words becomes ["jump", "run", "jump", "run"]
- No adjacent pairs share a common prefix (length 0)
- Removing index 2:
- words becomes ["jump", "run", "jump", "run"]
- No adjacent pairs share a common prefix (length 0)
- Removing index 3:
- words becomes ["jump", "run", "run", "run"]
- Longest adjacent pair is ["run", "run"] having a common prefix "run" (length 3)
- Removing index 4:
- words becomes ["jump", "run", "run", "jump"]
- Longest adjacent pair is ["run", "run"] having a common prefix "run" (length 3)
Example 2:
Input: words = ["dog","racer","car"]
Output: [0,0,0]
Explanation:
- Removing any index results in an answer of 0.
Constraints:
1 <= words.length <= 10^51 <= words[i].length <= 10^4words[i]consists of lowercase English letters.- The sum of
words[i].lengthis smaller than or equal10^5.
Solution
Approach: Prefix and Suffix Maximums
Let adj[i] be the longest common prefix of words[i] and words[i + 1]. When words[i] is removed, the adjacent pairs that remain are:
- the pairs entirely to the left,
adj[0..i-2], - the pairs entirely to the right,
adj[i+1..], - the new pair
words[i-1],words[i+1], if both exist.
Store prefix maximums and suffix maximums of adj, so the first two groups are one lookup each. Compute the new pair directly by comparing the two words. The answer for i is the largest of these values, or 0 if there is no pair.
Complexity: O(L) time, where L is the total length of all words (each comparison costs at most the shorter word), and O(n) extra space.
class Solution:
def longestCommonPrefix(self, words: List[str]) -> List[int]:
n = len(words)
def calc(s: str, t: str) -> int:
k = 0
while k < len(s) and k < len(t) and s[k] == t[k]:
k += 1
return k
# adj[i] = common prefix of words[i] and words[i + 1]
adj = [calc(words[i], words[i + 1]) for i in range(n - 1)]
pre = [0] * (n + 1) # pre[i] = max of adj[0..i-1]
for i in range(n - 1):
pre[i + 1] = max(pre[i], adj[i])
suf = [0] * (n + 1) # suf[i] = max of adj[i..n-2]
for i in range(n - 2, -1, -1):
suf[i] = max(suf[i + 1], adj[i])
ans = []
for i in range(n):
best = pre[max(i - 1, 0)] # pairs (j, j + 1) with j + 1 < i
best = max(best, suf[i + 1] if i + 1 < n else 0) # pairs starting after i
if 0 < i < n - 1:
best = max(best, calc(words[i - 1], words[i + 1]))
ans.append(best)
return ans