Longest Prefix Suffix
Submissions: 10772   Accuracy:

38.57%

  Difficulty: Medium   Marks: 4
     

Given a string of character, find the length of longest proper prefix which is also a proper suffix.
Example:
S = abab
lps is 2 because, ab.. is prefix and ..ab is also a suffix.

Input:
First line is T number of test cases. 1<=T<=100.
Each test case has one line denoting the string of length less than 100000.

Expected time compexity is O(N).

Output:
Print length of longest proper prefix which is also a proper suffix.

Example:
Input:
2
abab
aaaa

Output:
2
3

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: dhruvi


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.