<p>There is a country of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. In this country, there is a road connecting <b>every pair</b> of cities.</p>

<p>There are <code>m</code> friends numbered from <code>0</code> to <code>m - 1</code> 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 <strong>more than once</strong>, but the same city will not be listed consecutively.</p>

<p>Given an integer <code>n</code> and a 2D integer array <code>paths</code> where <code>paths[i]</code> is an integer array representing the path of the <code>i<sup>th</sup></code> friend, return <em>the length of the <strong>longest common subpath</strong> that is shared by <strong>every</strong> friend&#39;s path, or </em><code>0</code><em> if there is no common subpath at all</em>.</p>

<p>A <strong>subpath</strong> of a path is a contiguous sequence of cities within that path.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 5, paths = [[0,1,<u>2,3</u>,4],
                       [<u>2,3</u>,4],
                       [4,0,1,<u>2,3</u>]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The longest common subpath is [2,3].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 3, paths = [[0],[1],[2]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no common subpath shared by the three paths.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 5, paths = [[<u>0</u>,1,2,3,4],
                       [4,3,2,1,<u>0</u>]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The possible longest common subpaths are [0], [1], [2], [3], and [4]. All have a length of 1.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>m == paths.length</code></li>
	<li><code>2 &lt;= m &lt;= 10<sup>5</sup></code></li>
	<li><code>sum(paths[i].length) &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= paths[i][j] &lt; n</code></li>
	<li>The same city is not listed multiple times consecutively in <code>paths[i]</code>.</li>
</ul>

class Solution:
    def longestCommonSubpath(self, n: int, paths: List[List[int]]) -> int:
        # this is a problem with very large input test cases, so brute force and DP will never work
        # best solution will be to use binary search on answer range and then use rolling hash to efficiently find matches

        def helper(m):
            # returns True if there is a repeated subarray of length m
            r = 10 ** 5 + 1  # max num is 10^5
            q = 2 ** 63 + 1

            # precompute r^(m-1) % q
            rm = 1
            j = 1
            while j <= m - 1:
                rm = (rm * r) % q
                j += 1

            # have to loop on all paths, then hash all subarrays of length m
            # and save these in individual hashsets, which get merged into main hashset
            common_set = set()

            for i, path in enumerate(paths):
                hash_set = set()

                # calculate initial hash for path
                h = 0
                j = 0
                while j <= m - 1:
                    h = ((h * r) + path[j]) % q
                    j += 1

                hash_set.add(h)

                # find all other hashes

                # length - m + 1 since python range is non inclusive
                for j in range(1, len(path) - m + 1):
                    h = (h + q - (path[j - 1] * rm) % q) % q
                    h = ((h * r) + path[j + m - 1]) % q

                    hash_set.add(h)
                    # print("n1", h1, nums1[i:i+m])

                if i == 0: # 1st path, so just add to common
                    # initially common set is len 0, so dont use that, can cause bug
                    common_set = hash_set
                else: # else get intersection
                    common_set = common_set.intersection(hash_set) # this does not modify the set, remember

                if len(common_set) == 0: # if common set is still 0, means no intersection
                    break

            return len(common_set) > 0 # if len > 0, means we have intersection

        lo, hi = 0, min(len(path) for path in paths)  # max length can be the min among each path
        while lo < hi:
            mid = lo + (hi - lo + 1) // 2
            if helper(mid):  # if subarray length mid is possible, look higher
                lo = mid
            else:
                hi = mid - 1

        return lo
2236