Similar Problems
Similar Problems not available
Longest Common Subpath - Leetcode Solution
Companies:
LeetCode: Longest Common Subpath Leetcode Solution
Difficulty: Hard
Topics: binary-search array
The Longest Common Subpath problem on Leetcode asks us to find the length of the longest common subpath within two or more paths. Here, a subpath is defined as a contiguous sequence of nodes in a path, and a common subpath is a subpath that appears in all paths.
We can use binary search and hashing to solve this problem. We can start by setting our search space to the length of the shortest path and the length of the longest common subpath is always a value in this search space.
We can then use hashing to check if a common subpath of length k exists. We create a hash for each subpath of length k in the first path and compare it with all subpaths of the same length in the other paths. If a match is found, we increase k by one and repeat until we have found the longest common subpath.
Here is the detailed step-by-step solution:
- First, we can calculate the length of the shortest path among all given paths. Let this length be 'L'.
- We then define a search space, which is a range between [1, L]. We can use binary search to find the value of the longest common subpath within this range.
- In each iteration of the binary search, we take the mid-point of the search space, called 'mid'.
- We then create a set of hashes for each subpath of length mid in the first path. We can use any hashing technique to create a hash for each subpath.
- Next, we check if any subpath of the same length in the other paths exists in the hash set. If any match is found, we increase 'mid' by one and set the search space to [mid+1, R], where R is the length of the shortest path.
- If no match is found, we set the search space to [L, mid-1].
- We repeat the loop until the search space between L and R is empty.
- Finally, we return the maximum subpath length found during the loop.
Here is the implementation of the above approach in Python:
def longestCommonSubpath(n: int, paths: List[List[int]]) -> int:
L = min([len(path) for path in paths])
low, high = 1, L # search space
mod = 10**9 + 7 # prime number for hashing
ans = 0
# binary search
while low <= high:
mid = (low + high) // 2
visited = set()
flag = False
for i in range(n):
h = 0
for j in range(mid):
h = (h * 26 + paths[i][j]) % mod
visited.add(h)
# check for common subpath
for i in range(n):
h = 0
for j in range(mid):
h = (h * 26 + paths[i][j]) % mod
if h not in visited:
flag = True
break
# update search space based on subpath found or not
if flag:
high = mid - 1
else:
ans = mid
low = mid + 1
return ans
The time complexity of the above approach is O(NlogLlog(NL)), where N is the number of paths and L is the length of the shortest path. This is because we do binary search and for each subpath of length k, we need to calculate its hash value for all paths, which takes O(NL) time in total. The space complexity is O(NL) for the set of hashes created for each subpath.
Longest Common Subpath Solution Code
1