Longest Common Subpath - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: n = 5, paths = [[0,1,2,3,4],[2,3,4],[4,0,3,2,1,5]]
Output: 1
💡 Note: The longest common subpath has length 1. City 4 appears in all three paths: at paths[0][4], paths[1][2], and paths[2][0]. No subpath of length 2 or more appears in all paths.
Example 2 — No Common Subpath
$ Input: n = 3, paths = [[0,1,2],[1,2,0],[2,0,1]]
Output: 0
💡 Note: No subpath of length > 0 appears in all three paths due to different orderings
Example 3 — Single City Common
$ Input: n = 4, paths = [[0,1,2],[2,3],[1,2,3]]
Output: 1
💡 Note: City 2 appears in all paths: paths[0][2], paths[1][0], paths[2][1]. So longest common subpath has length 1.

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ m ≤ 103
  • 1 ≤ paths[i].length ≤ 105
  • 0 ≤ paths[i][j] < n
  • All paths[i] are distinct

Visualization

Tap to expand
Longest Common Subpath INPUT n = 5 (cities: 0-4) Path 0: 0 1 2 3 4 Path 1: 2 3 4 Path 2: 4 0 3 2 1 5 City Graph (n=5) 0 1 2 3 4 ALGORITHM STEPS 1 Binary Search Length Search for max length L range: [0, min_path_len] 2 Rolling Hash Hash subpaths of length L h = (h*base + c) mod p 3 Hash Set Intersection Find common hashes across all paths 4 Adjust Search If common exists: L++ Else: L-- Hash Sets for L=2: [0,1][1,2][2,3][3,4] [2,3][3,4] [4,0][0,3][3,2] [2,1][1,5] Common: [2,3] [3,4] FINAL RESULT Output: 2 Longest common subpath has length 2 Common Subpaths Found: [2, 3] [3, 4] Verification: Path 0: 0,1, 2,3 ,4 Path 1: 2,3 ,4 Path 2: 4,0, 3,2 ,1,5 (3,2 is reverse of 2,3 but [3,2] matches!) OK - Verified Key Insight: Binary search on answer length combined with rolling hash (Rabin-Karp) enables efficient O(n*m*log(m)) checking of common subpaths. The hash converts subpath comparison from O(L) to O(1). Use multiple hash functions or careful modulus to avoid hash collisions for correctness. TutorialsPoint - Longest Common Subpath | Rolling Hash + Binary Search Approach
Asked in
Google 15 Facebook 8 Amazon 5
8.2K Views
Medium Frequency
~45 min Avg. Time
267 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen