Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to find the longest repeating substring in a string with Python

2 Answers

0 votes
def longest_common_prefix(sub1, sub2):  
    n = min(len(sub1), len(sub2));  
       
    for i in range(0, n):  
        if (sub1[i] != sub2[i]):  
            return sub1[0:i];  
               
    return sub1[0:n];  
      
s = "pythonphpjavacdartcppjavacsharp";  
lrs = "";  
size = len(s);  
   
for i in range(0, size):  
  for j in range(i+1, size):  
    lcp = longest_common_prefix(s[i:size], s[j:size]);  
    if (len(lcp) > len(lrs)):  
        lrs = lcp;    
           
print(lrs);  
   
   
   
   
'''
run:
   
pjavac
   
'''

 



answered Jan 16, 2023 by avibootz
edited Jan 17, 2023 by avibootz
0 votes
def longest_common_prefix(sub1, sub2):  
    n = min(len(sub1), len(sub2));  
     
    for i in range(0, n):  
        if (sub1[i] != sub2[i]):  
            return sub1[0:i];  
             
    return sub1[0:n];  
    
def longest_repeating_substring(s):
    suffixes = []
    size = len(s)
    
    for i in range(size):
        suffixes.append(s[i:size])
 
    suffixes.sort()
 
    max_len = 0
    for sub1, sub2 in zip(suffixes, suffixes[1:]):
        lcp = longest_common_prefix(sub1, sub2)
        if len(lcp) > max_len:
            max_len = len(lcp)
            lrs = lcp
 
    return lrs
    
s = "pythonphpjavacdartcppjavacsharp";  
 
print(longest_repeating_substring(s));  
 
 
 
 
'''
run:
 
pjavac
 
'''
 

 



answered Jan 16, 2023 by avibootz
edited Jan 17, 2023 by avibootz
...