Find the longest non-empty prefix of a string that is also a suffix, without using the whole string itself.
Given a string s, return the longest proper prefix of s that is also a suffix of s.
A proper prefix is a prefix that is not equal to the entire string. A suffix is a substring that ends at the last character.
If no such prefix exists, return an empty string.
Your task is to find the longest prefix of s that appears again at the end of s.
s.s that is also a suffix."".s consists of lowercase English letters.s itself.Example 1
Input
s = "level"
Output
"l"
Explanation
The prefixes are "l", "le", "lev", "leve". The suffixes are "l", "el", "vel", "evel". The longest string that is both a prefix and a suffix is "l".
Example 2
Input
s = "ababab"
Output
"abab"
Explanation
"abab" is both a prefix and a suffix, and it is the longest such proper prefix.
Example 3
Input
s = "leetcodeleet"
Output
"leet"
Explanation
The prefix "leet" also appears as the suffix, and no longer proper prefix matches.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.