Practice Longest Common Subpath
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Longest Common Subpath
Problem Statement
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 ith 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.
Source: Longest Common Subpath on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
n = 5, paths = [[0,1,2,3,4], [2,3,4], [4,0,1,2,3]]
2
# Example 2
n = 3, paths = [[0],[1],[2]]
0
# Example 3
n = 5, paths = [[0,1,2,3,4], [4,3,2,1,0]]
1
Constraints
Related LeetCode Problems
Practice more problems similar to Longest Common Subpath.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.