3598. Longest Common Prefix Between Adjacent Strings After Removals Medium
1/**
2 * [3598] Longest Common Prefix Between Adjacent Strings After Removals
3 *
4 * You are given an array of strings words. For each index i in the range [0, words.length - 1], perform the following steps:
5 *
6 * Remove the element at index i from the words array.
7 * Compute the length of the longest common <span data-keyword="string-prefix">prefix</span> among all adjacent pairs in the modified array.
8 *
9 * 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.
10 *
11 * <strong class="example">Example 1:
12 * <div class="example-block">
13 * Input: <span class="example-io">words = ["jump","run","run","jump","run"]</span>
14 * Output: <span class="example-io">[3,0,0,3,3]</span>
15 * Explanation:
16 *
17 * Removing index 0:
18 *
19 * words becomes ["run", "run", "jump", "run"]
20 * Longest adjacent pair is ["run", "run"] having a common prefix "run" (length 3)
21 *
22 *
23 * Removing index 1:
24 *
25 * words becomes ["jump", "run", "jump", "run"]
26 * No adjacent pairs share a common prefix (length 0)
27 *
28 *
29 * Removing index 2:
30 *
31 * words becomes ["jump", "run", "jump", "run"]
32 * No adjacent pairs share a common prefix (length 0)
33 *
34 *
35 * Removing index 3:
36 *
37 * words becomes ["jump", "run", "run", "run"]
38 * Longest adjacent pair is ["run", "run"] having a common prefix "run" (length 3)
39 *
40 *
41 * Removing index 4:
42 *
43 * words becomes ["jump", "run", "run", "jump"]
44 * Longest adjacent pair is ["run", "run"] having a common prefix "run" (length 3)
45 *
46 *
47 * </div>
48 * <strong class="example">Example 2:
49 * <div class="example-block">
50 * Input: <span class="example-io">words = ["dog","racer","car"]</span>
51 * Output: <span class="example-io">[0,0,0]</span>
52 * Explanation:
53 *
54 * Removing any index results in an answer of 0.
55 * </div>
56 *
57 * Constraints:
58 *
59 * 1 <= words.length <= 10^5
60 * 1 <= words[i].length <= 10^4
61 * words[i] consists of lowercase English letters.
62 * The sum of words[i].length is smaller than or equal 10^5.
63 *
64 */
65pub struct Solution {}
66
67// problem: https://leetcode.com/problems/longest-common-prefix-between-adjacent-strings-after-removals/
68// discuss: https://leetcode.com/problems/longest-common-prefix-between-adjacent-strings-after-removals/discuss/?currentPage=1&orderBy=most_votes&query=
69
70// submission codes start here
71
72impl Solution {
73 pub fn longest_common_prefix(words: Vec<String>) -> Vec<i32> {
74 vec![]
75 }
76}
77
78// submission codes end
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn test_3598() {
86 }
87}
88Back
© 2026 bowen.ge All Rights Reserved.