1923. Longest Common Subpath Hard

@problem@discussion
#Array#Binary Search#Rolling Hash#Suffix Array#Hash Function



1/**
2 * [1923] Longest Common Subpath
3 *
4 * There is a country of n cities numbered from 0 to n - 1. In this country, there is a road connecting every pair of cities.
5 * There are m friends numbered from 0 to m - 1 who are traveling through the country. Each one of them will take a path consisting of some cities. Each path is represented by an integer array that contains the visited cities in order. The path may contain a city more than once, but the same city will not be listed consecutively.
6 * Given an integer n and a 2D integer array paths where paths[i] is an integer array representing the path of the i^th friend, return the length of the longest common subpath that is shared by every friend's path, or 0 if there is no common subpath at all.
7 * A subpath of a path is a contiguous sequence of cities within that path.
8 *  
9 * Example 1:
10 * 
11 * Input: n = 5, paths = [[0,1,<u>2,3</u>,4],
12 *                        [<u>2,3</u>,4],
13 *                        [4,0,1,<u>2,3</u>]]
14 * Output: 2
15 * Explanation: The longest common subpath is [2,3].
16 * 
17 * Example 2:
18 * 
19 * Input: n = 3, paths = [[0],[1],[2]]
20 * Output: 0
21 * Explanation: There is no common subpath shared by the three paths.
22 * 
23 * Example 3:
24 * 
25 * Input: n = 5, paths = [[<u>0</u>,1,2,3,4],
26 *                        [4,3,2,1,<u>0</u>]]
27 * Output: 1
28 * Explanation: The possible longest common subpaths are [0], [1], [2], [3], and [4]. All have a length of 1.
29 *  
30 * Constraints:
31 * 
32 * 	1 <= n <= 10^5
33 * 	m == paths.length
34 * 	2 <= m <= 10^5
35 * 	sum(paths[i].length) <= 10^5
36 * 	0 <= paths[i][j] < n
37 * 	The same city is not listed multiple times consecutively in paths[i].
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/longest-common-subpath/
43// discuss: https://leetcode.com/problems/longest-common-subpath/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn longest_common_subpath(n: i32, paths: Vec<Vec<i32>>) -> i32 {
49        0
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_1923() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.