
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to find length of longest repeating substring in a string in Python
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 −
- Define a function lcs() . This will take s1, s2
- n := minimum of size of s1 and size of s2
- for i in range 0 to n - 1, do
- if s1[i] is not same as s2[i], then
- return substring of s1[from index 0 to i-1]
- if s1[i] is not same as s2[i], then
- return substring of s1[from index 0 to n - 1]
- From the main method, do the following −
- suffixes := a new list
- n := size of s
- max_len := 0
- for i in range 0 to n - 1, do
- insert (substring of s[from index i to n - 1]) at the end of suffixes
- sort the list suffixes
- for each item a from suffixes and b from substring of suffixes[from index 1 to end], do
- rtr := lcs(a, b)
- if size of rtr > max_len, then
- max_len := size of rtr
- return max_len
Example
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))
Input
"abdgoalputabdtypeabd"
Output
3
- Related Articles
- Program to find length of longest palindromic substring in Python
- Program to find length of longest consecutively increasing substring in Python
- Longest Substring Without Repeating Characters in Python
- Program to find length of longest common substring in C++
- Longest Repeating Substring in C++
- Program to find length of longest substring with 1s in a binary string after one 0-flip in Python
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find length of longest substring with even vowel counts in Python
- Program to find length of longest substring which contains k distinct characters in Python
- How to find the length of the longest substring from the given string without repeating the characters using C#?
- Program to find longest awesome substring in Python
- Program to find the length of longest substring which has two distinct elements in Python
- Program to find length of longest substring with character count of at least k in Python
- Program to find length of longest valid parenthesis from given string in Python
- Program to find maximum k-repeating substring from sequence in Python

Advertisements