Imagine a country with n cities numbered from 0 to n-1, where every city is connected to every other city by roads. You have m friends who are each traveling through this country, taking different routes.
Each friend's journey is represented as an array of cities they visit in order. For example, if a friend visits cities [1, 3, 2, 4], they go from city 1 to 3, then to 2, then to 4. A friend might visit the same city multiple times during their journey, but won't visit the same city twice in a row.
Your task is to find the longest common subpath that all friends share during their travels. A subpath is a contiguous sequence of cities within a path.
Goal: Return the length of the longest subpath that appears in every friend's journey, or 0 if no such common subpath exists.
Input & Output
Visualization
Time & Space Complexity
n paths, each of length m, checking all subpaths of all lengths L
Space to store current subpath being checked
Constraints
- 1 β€ n β€ 105
- 2 β€ m β€ 103 (m is the number of friends)
- 1 β€ paths[i].length β€ 105
- 0 β€ paths[i][j] < n
- Important: The same city will not appear consecutively in a path
- 1 β€ sum(paths[i].length) β€ 105