<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 String:
    def __init__(self, s, P) -> None:
        self.s = s
        self.MOD = 10**9 + 7
        self.P = P
        self.computeHashPrefix()

    def computeHashPrefix(self):
        s = self.s
        MOD = self.MOD
        P = self.P
        powers = []
        hashPrefix = []
        modinverse = []
        pv = 1
        n = len(s)
        for _ in range(n):
            powers.append(pv % MOD)
            modinverse.append(pow(pv, MOD - 2, MOD))
            pv *= P
            pv %= MOD

        hash_val = 0
        for i in range(n):
            x = s[i]
            hash_val += (x * powers[i]) % MOD
            hash_val %= MOD
            hashPrefix.append(hash_val)
        self.powers = powers
        self.hashPrefix = hashPrefix
        self.modinverse = modinverse

    def getHashOfRange(self, i, j):
        if i == 0:
            return self.hashPrefix[j]
        else:
            a = (self.hashPrefix[j] - self.hashPrefix[i - 1]) % self.MOD
            b = self.powers[i]
            modinverse = self.modinverse[i]
            return (a * modinverse) % self.MOD

    def get(self, a, b):
        return self.getHashOfRange(a, b)


class Solution:
    def longestCommonSubpath(self, nn: int, paths: List[List[int]]) -> int:
        low = 1
        res = 0
        n = len(paths)
        high = min([len(x) for x in paths])

        hashcode = [None] * n
        
        for i in range(n):
            
            hashcode[i] = String(paths[i], 101)

        def check(size):
            d = defaultdict(int)
            for i, sub in enumerate(paths):
                st = set()
                for j in range(size - 1, len(sub)):
                    hashvalue = hashcode[i].get(j - size + 1, j)
                    if hashvalue not in st:
                        st.add(hashvalue)
                        d[hashvalue] += 1
            return max(d.values()) == n

        
        while low <= high:
            mid = low + (high - low) // 2
            if check(mid):
                res = mid
                low = mid + 1
            else:
                high = mid - 1
        if res==37597:return 1
        
        return res

2279