forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_1392.java
53 lines (52 loc) · 1.8 KB
/
_1392.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.fishercoder.solutions;
/**
* 1392. Longest Happy Prefix
*
* 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:
* 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:
* Input: s = "ababab"
* Output: "abab"
* Explanation: "abab" is the largest prefix which is also suffix. They can overlap in the original string.
*
* Example 3:
* Input: s = "leetcodeleet"
* Output: "leet"
*
* Example 4:
* Input: s = "a"
* Output: ""
*
* Constraints:
* 1 <= s.length <= 10^5
* s contains only lowercase English letters.
* */
public class _1392 {
public static class Solution1 {
/**credit: https://leetcode.com/problems/longest-happy-prefix/discuss/547446/C%2B%2BJava-Incremental-Hash-and-DP*/
public String longestPrefix(String s) {
int times = 2;
long prefixHash = 0;
long suffixHash = 0;
long multiplier = 1;
long len = 0;
long mod = 1000000007;//use some large prime as a modulo to avoid overflow errors, e.g. 10 ^ 9 + 7.
for (int i = 0; i < s.length() - 1; i++) {
prefixHash = (prefixHash * times + s.charAt(i)) % mod;
suffixHash = (multiplier * s.charAt(s.length() - i - 1) + suffixHash) % mod;
if (prefixHash == suffixHash) {
len = i + 1;
}
multiplier = multiplier * times % mod;
}
return s.substring(0, (int) len);
}
}
}