Skip to content

Commit 8468524

Browse files
refactor 1392
1 parent c1218b3 commit 8468524

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11-
|1392|[Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1392.java) | |Hard|String|
11+
|1392|[Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1392.java) | |Hard|String, Rolling Hash|
1212
|1390|[Four Divisors](https://leetcode.com/problems/four-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1390.java) | |Medium|Math|
1313
|1389|[Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1389.java) | |Easy|Array|
1414
|1388|[Pizza With 3n Slices](https://leetcode.com/problems/pizza-with-3n-slices/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1388.java) | |Hard|DP|

src/main/java/com/fishercoder/solutions/_1392.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ public class _1392 {
3333
public static class Solution1 {
3434
/**credit: https://leetcode.com/problems/longest-happy-prefix/discuss/547446/C%2B%2BJava-Incremental-Hash-and-DP*/
3535
public String longestPrefix(String s) {
36-
long headHash = 0;
37-
long tailHash = 0;
36+
int times = 2;
37+
long prefixHash = 0;
38+
long suffixHash = 0;
3839
long multiplier = 1;
3940
long len = 0;
4041
long mod = 1000000007;//use some large prime as a modulo to avoid overflow errors, e.g. 10 ^ 9 + 7.
4142
for (int i = 0; i < s.length() - 1; i++) {
42-
headHash = (headHash * 26 + s.charAt(i)) % mod;
43-
tailHash = (multiplier * s.charAt(s.length() - i - 1) + tailHash) % mod;
44-
if (headHash == tailHash) {
43+
prefixHash = (prefixHash * times + s.charAt(i)) % mod;
44+
suffixHash = (multiplier * s.charAt(s.length() - i - 1) + suffixHash) % mod;
45+
if (prefixHash == suffixHash) {
4546
len = i + 1;
4647
}
47-
multiplier = multiplier * 26 % mod;
48+
multiplier = multiplier * times % mod;
4849
}
4950
return s.substring(0, (int) len);
5051
}

src/test/java/com/fishercoder/_1392Test.java

Lines changed: 5 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy