<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>

from collections import defaultdict
class Solution:
    # Time: O()
    # Space: O(), where m = len(paths), k = mean(len(paths[i]))
    def longestCommonSubpath(self, n: int, paths: List[List[int]]) -> int:
        
        m = len(paths)
        min_len = min([len(paths[i]) for i in range(m)])
        MOD = (10 ** 9 + 7) ** 2

        '''
        # MLE
        hashes = [{j:0 for j in range(len(paths[i])+1)} for i in range(m)]
        for i in range(m):
            for j in range(1, len(paths[i]) + 1):
                element = paths[i][j - 1]
                # Incorporating both the element and its position in the hash
                hashes[i][j] = hashes[i][j - 1] * 31 + hash(element)
        '''

        # TLE
        def check(l):
            # Returns 1 if there exist a common path length l
            h_tot = set()
            for i in range(m):

                h_curr = set()
                dim = len(paths[i])
                curr = '-'.join([paths[i][j] for j in range(l)])
                h_curr.add(hash(curr))
                # h_curr.add(curr)

                for j in range(l, dim):
                    curr += '-' + paths[i][j]
                    curr = curr[len(paths[i][j-l])+1:]
                    h_curr.add(hash(curr))
                    # h_curr.add(curr)

                if i == 0:
                    h_tot = h_curr
                if i > 0:
                    h_tot = h_tot.intersection(h_curr) 
                    if not h_tot:
                        return 0
            return len(h_tot) > 0
    
        # MLE
        def check2(l):
            # Returns 1 if there exist a common path length l
            h_tot = set()
            for i in range(m):

                h_curr = set()
                dim = len(paths[i])
                curr = hashes[i][l]
                h_curr.add(curr)

                for j in range(l+1, dim+1):
                    # curr = hashes[i][j] - hashes[i][j-l]
                    curr = hashes[i][j] - hashes[i][j-l] * (31 ** (l))
                    h_curr.add(curr)

                if i == 0:
                    h_tot = h_curr
                if i > 0:
                    h_tot = h_tot.intersection(h_curr) 
                    if not h_tot:
                        return 0
            return len(h_tot) > 0

        def check3(l):
            # Returns 1 if there exist a common path length l
            h_tot = defaultdict(set)
            for i in range(m):

                dim = len(paths[i])

                # Compute current hash up to l
                curr = 0
                for j in range(1, l+1):
                    element = paths[i][j - 1]
                    # Incorporating both the element and its position in the hash
                    curr = curr * 31 + hash(element)
                curr = curr % MOD
                h_tot[curr].add(i)

                base = 31 ** (l - 1)
                for j in range(l+1, dim+1):
                    # Remove the contribution of the element at position j-l
                    curr -= hash(paths[i][j - l - 1]) * base
                    # Add the contribution of the new element at position j
                    curr = curr * 31 + hash(paths[i][j-1])
                    curr = curr % MOD
                    h_tot[curr].add(i)

            return max([len(people) for people in h_tot.values()]) >= m

        def get_hash(path,k):
            h, base, ans = 0, 1<<17, set()

            for i in range(len(path)):
                h = h * base + path[i]

                if i >= k:
                    h -= path[i-k]*pow(base, k, MOD)

                h = h % MOD

                if i >= k-1:
                    ans.add(h)

            return ans


        i, j = 0, min_len
        res = 0
        while i <= j:
            mid = (i+j) // 2
            feasible = len(set.intersection(*[get_hash(p,mid) for p in paths]))
            if feasible:
                i = mid + 1
                res = max(res, mid)
            else:
                j = mid - 1
        return res
3316