A string is called a happy prefix if is a non-empty prefix which is also a suffix (excluding itself).
Given a string s. Return the longest happy prefix of s .
Return an empty string if no such prefix exists.
Example 1:
1 2 3
Input: s = "level" Output: "l" Explanation: s contains 4 prefix excluding itself ("l", "le", "lev", "leve"), and suffix ("l", "el", "vel", "evel"). The largest prefix which is also suffix is given by "l".
Example 2:
1 2 3
Input: s = "ababab" Output: "abab" Explanation: "abab" is the largest prefix which is also suffix. They can overlap in the original string.
Example 3:
1 2
Input: s = "leetcodeleet" Output: "leet"
Example 4:
1 2
Input: s = "a" Output: ""
Constraints:
1 <= s.length <= 10^5
s contains only lowercase English letters.
分析
思路1: 采用KMP算法求最长公共前后缀中的部分代码来进行求解。prefixTable[]。
思路2: 使用字符串hash,取一固定制P(一般取P = 131 或者 P=1331),把字符串看作P进制数,并分配一个大于0的数值,代表每种字符。一般来说,我们分配的数值都远小于P。例如对于小写字符构成的字符串,可以令 a = 1,b=2,.. z = 26。取一固定制M,求出该P进制数对M的余数,作为该字符串的Hash值。通常我们选取 $ M = 2^{64}$ ,即直接使用 unsigned long long 类型存储这个Hash值,在计算时不处理算数的益出问题,产生溢出相当于自动对 $2^{64}$ 取模,这样可以避免低效的取模运算。