Longest Happy Prefix
A happy prefix of a string is a non-empty prefix that is also a suffix, but not the whole string itself. Given a string s, return its longest happy prefix, or the empty string if none exists.
Open official problem prompt ↗Find the longest string that is simultaneously a proper prefix and a proper suffix of s.
Like folding a strip of paper so its left edge overlaps its right edge as much as possible without covering the whole strip; the overlap length is the answer.
- Input
- s = "level"
- Output
- "l"
- Why
- "l" is both the first and last character of "level", and no longer prefix (le, lev, leve) also appears as a suffix.
1 <= s.length <= 10^5s contains only lowercase English letters