3598. Longest Common Prefix Between Adjacent Strings After Removals
Description
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 thewords
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.
A prefix of a string is a substring that starts from the beginning of the string and extends to any point within it.
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)
- words becomes
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 <= 105
1 <= words[i].length <= 104
words[i]
consists of lowercase English letters.- The sum of
words[i].length
is smaller than or equal105
.
Solutions
Solution 1: Ordered Set
We define a function \(\textit{calc}(s, t)\), which calculates the length of the longest common prefix between strings \(s\) and \(t\). We can use an ordered set to maintain the longest common prefix lengths of all adjacent string pairs.
Define a function \(\textit{add}(i, j)\), which adds the longest common prefix length of the string pair at indices \(i\) and \(j\) to the ordered set. Define a function \(\textit{remove}(i, j)\), which removes the longest common prefix length of the string pair at indices \(i\) and \(j\) from the ordered set.
First, we compute the longest common prefix lengths for all adjacent string pairs and store them in the ordered set. Then, for each index \(i\), we perform the following steps:
- Remove the longest common prefix length of the string pair at indices \(i\) and \(i + 1\).
- Remove the longest common prefix length of the string pair at indices \(i - 1\) and \(i\).
- Add the longest common prefix length of the string pair at indices \(i - 1\) and \(i + 1\).
- Add the current maximum value in the ordered set (if it exists and is greater than 0) to the answer.
- Remove the longest common prefix length of the string pair at indices \(i - 1\) and \(i + 1\).
- Add the longest common prefix length of the string pair at indices \(i - 1\) and \(i\).
- Add the longest common prefix length of the string pair at indices \(i\) and \(i + 1\).
In this way, after removing each string, we can quickly compute the longest common prefix length between adjacent string pairs.
The time complexity is \(O(L + n \times \log n)\), and the space complexity is \(O(n)\), where \(L\) is the total length of all strings and \(n\) is the number of strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|