Given a string s, return the longest happy prefix of s. A happy prefix is a string that is both a proper prefix and a proper suffix.
If no such prefix exists, return an empty string "".
Input: s = "ababab"
Output: "abab"
Explanation: "abab" is the longest prefix which is also suffix. They can overlap in the original string.
Input: s = "aaaa"
Output: "aaa"
Explanation: "aaa" is the longest prefix which is also a suffix in the string "aaaa".
Input: s = "abc"
Striver and the team have worked tirelessly over the past few days to bring this to you. Please bear with us a little more as we finalize everything.
Q: Can we extend this approach to find multiple happy prefixes?
A: Yes! We can track all non-zero LPS values leading to multiple valid prefixes.
Q: How does this compare to Z-algorithm for pattern matching?
A: LPS (KMP) is best for prefix-suffix problems, while Z-algorithm is better for substring matching.