Suppose we have a lowercase string s, we have to find the length of the longest substring that occurs at least twice in s. If we cannot find such string, return 0.
So, if the input is like s = "abdgoalputabdtypeabd", then the output will be 3, because the longest substring that occurs more than once is "abd".
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def lcs(s1, s2): n = min(len(s1), len(s2)) for i in range(n): if s1[i] != s2[i]: return s1[:i] return s1[:n] def solve(s): suffixes =  n = len(s) max_len = 0 for i in range(n): suffixes.append(s[i:n]) suffixes.sort() for a, b in zip(suffixes, suffixes[1:]): rtr = lcs(a, b) if len(rtr) > max_len: max_len = len(rtr) return max_len s = "abdgoalputabdtypeabd" print(solve(s))