Longest prefix suffix Infosys springboard coding question with solution

Problem Statement :

Given a non-empty sting instr containing only alphabets, print the length of the longest prefix in instr which is same as the suffix.

The prefix and suffix should not be overlap in instr

Print -1 if no prefix exists which is also the suffix without overlap.

Question :

Code Solution :

				
					s=input()
count=0
for i in range(len(s)//2,0,-1):
    if s[0:i]==s[len(s)-i:]:
        print(len(s[0:i]))
        break

				
			

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *