Class Solution
- java.lang.Object
-
- g1901_2000.s1923_longest_common_subpath.Solution
-
public class Solution extends Object
1923 - Longest Common Subpath.Hard
There is a country of
ncities numbered from0ton - 1. In this country, there is a road connecting every pair of cities.There are
mfriends numbered from0tom - 1who 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.Given an integer
nand a 2D integer arraypathswherepaths[i]is an integer array representing the path of theithfriend, return the length of the longest common subpath that is shared by every friend’s path, or0if there is no common subpath at all.A subpath of a path is a contiguous sequence of cities within that path.
Example 1:
Input: n = 5, paths = [[0,1,2,3,4], [2,3,4], [4,0,1,2,3]]
Output: 2
Explanation: The longest common subpath is [2,3].
Example 2:
Input: n = 3, paths = [[0],[1],[2]]
Output: 0
Explanation: There is no common subpath shared by the three paths.
Example 3:
Input: n = 5, paths = [[0,1,2,3,4], [4,3,2,1,0]]
Output: 1
Explanation: The possible longest common subpaths are [0], [1], [2], [3], and [4]. All have a length of 1.
Constraints:
1 <= n <= 105m == paths.length2 <= m <= 105sum(paths[i].length) <= 1050 <= paths[i][j] < n- The same city is not listed multiple times consecutively in
paths[i].
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intlongestCommonSubpath(int n, int[][] paths)
-