Longest Common Subpath Problem
Description
LeetCode Problem 1923.
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.
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.
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.
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 <= 10^5m == paths.length2 <= m <= 10^5sum(paths[i].length) <= 10^50 <= paths[i][j] < n- The same city is not listed multiple times consecutively in
paths[i].
Solution
Approach: Binary Search on the Length + Rolling Hash
If a subpath of length L is common to all paths, then so is every shorter subpath obtained by trimming its ends. So "a common subpath of length L exists" is monotone in L, and we can binary search the largest such L between 0 and the length of the shortest path.
To test one length L, hash every window of length L in each path with a polynomial rolling hash. Prefix hashes are computed once per path, so a window hash is pre[i+L] - pre[i] * base^L. Keep a set of the window hashes of the first path, then intersect it with the window hashes of each following path. If the set ever becomes empty, no common subpath of length L exists.
Hash collisions are the only risk, so use a large modulus (261 - 1) or two moduli combined into one key.
Complexity: O(S log(min length)) expected time, where S is the total length of all paths, and O(S) space.
class Solution:
def longestCommonSubpath(self, n: int, paths: List[List[int]]) -> int:
mod = (1 << 61) - 1
base = 1000003
longest = max(len(p) for p in paths)
pw = [1] * (longest + 1)
for i in range(1, longest + 1):
pw[i] = pw[i - 1] * base % mod
pre = [] # pre[k][i] = hash of the first i cities of paths[k]
for p in paths:
h = [0]
for x in p:
h.append((h[-1] * base + x + 1) % mod)
pre.append(h)
def windows(h, length):
return {(h[i + length] - h[i] * pw[length]) % mod for i in range(len(h) - length)}
def check(length):
common = windows(pre[0], length)
for h in pre[1:]:
common &= windows(h, length)
if not common:
return False
return True
lo, hi = 0, min(len(p) for p in paths)
while lo < hi:
mid = (lo + hi + 1) >> 1
if check(mid):
lo = mid
else:
hi = mid - 1
return lo