Longest Common Subpath - Problem

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

example_1.py β€” Basic Case
$ Input: n = 5, paths = [[0,1,2,3,4],[2,3,4],[4,0,1,2,3]]
β€Ί Output: 2
πŸ’‘ Note: The longest common subpath is [2,3] which has length 2. All three friends visit cities 2 and 3 consecutively in their journeys.
example_2.py β€” No Common Subpath
$ Input: n = 3, paths = [[0],[1],[2]]
β€Ί Output: 0
πŸ’‘ Note: Each friend visits only one city, and they all visit different cities. There is no common subpath shared by all friends.
example_3.py β€” Single City Common
$ Input: n = 5, paths = [[0,1,2,3,4],[4,3,2,1,0]]
β€Ί Output: 1
πŸ’‘ Note: Both friends visit cities 0, 1, 2, 3, and 4, but in reverse order. The longest common subpath is any single city they both visit, so the answer is 1.

Visualization

Tap to expand
πŸ—ΊοΈ Friend Travel Route AnalysisπŸ‘₯ Friends' Travel Routes:ABCDFriend 1: Aβ†’Bβ†’Cβ†’DBCEFriend 2: Bβ†’Cβ†’ECDFFriend 3: Cβ†’Dβ†’FπŸ” Binary Search Process:Length Range:Min: 0Max: 3Try Mid = 1πŸ” Rolling Hash Magic:Bβ†’CHash:h_BCCβ†’DHash:h_CDβœ… Common route found: Bβ†’C (Length 2)🎯 Final Result:Longest Common SubpathLength: 2 cities
Understanding the Visualization
1
Collect all travel data
Each friend provides their complete travel route as a sequence of cities visited
2
Binary search on route length
Instead of checking every possible route length, we intelligently narrow down using binary search
3
Generate route signatures
For each candidate length, we create unique 'fingerprints' (hashes) for all possible route segments
4
Find common signatures
We keep only the route signatures that appear in EVERY friend's journey
5
Optimize the search
If we find a common route of length k, we try to find longer ones; otherwise, we try shorter ones
Key Takeaway
🎯 Key Insight: By using binary search on the length and rolling hash for efficient comparison, we can find the longest common travel route in optimal time, just like finding the longest shared journey among friends' road trips!

Time & Space Complexity

Time Complexity
⏱️
O(n * mΒ³ * LΒ³)

n paths, each of length m, checking all subpaths of all lengths L

n
2n
βœ“ Linear Growth
Space Complexity
O(L)

Space to store current subpath being checked

n
2n
βœ“ Linear Space

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
Asked in
Google 85 Amazon 72 Meta 68 Microsoft 45
58.3K Views
High Frequency
~35 min Avg. Time
1.8K 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