Skip to content

Commit c1218b3

Browse files
add 1392
1 parent 865c9f2 commit c1218b3

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +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|
1112
|1390|[Four Divisors](https://leetcode.com/problems/four-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1390.java) | |Medium|Math|
1213
|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|
1314
|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|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1392. Longest Happy Prefix
5+
*
6+
* A string is called a happy prefix if is a non-empty prefix which is also a suffix (excluding itself).
7+
* Given a string s. Return the longest happy prefix of s .
8+
* Return an empty string if no such prefix exists.
9+
*
10+
* Example 1:
11+
* Input: s = "level"
12+
* Output: "l"
13+
* 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".
14+
*
15+
* Example 2:
16+
* Input: s = "ababab"
17+
* Output: "abab"
18+
* Explanation: "abab" is the largest prefix which is also suffix. They can overlap in the original string.
19+
*
20+
* Example 3:
21+
* Input: s = "leetcodeleet"
22+
* Output: "leet"
23+
*
24+
* Example 4:
25+
* Input: s = "a"
26+
* Output: ""
27+
*
28+
* Constraints:
29+
* 1 <= s.length <= 10^5
30+
* s contains only lowercase English letters.
31+
* */
32+
public class _1392 {
33+
public static class Solution1 {
34+
/**credit: https://leetcode.com/problems/longest-happy-prefix/discuss/547446/C%2B%2BJava-Incremental-Hash-and-DP*/
35+
public String longestPrefix(String s) {
36+
long headHash = 0;
37+
long tailHash = 0;
38+
long multiplier = 1;
39+
long len = 0;
40+
long mod = 1000000007;//use some large prime as a modulo to avoid overflow errors, e.g. 10 ^ 9 + 7.
41+
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) {
45+
len = i + 1;
46+
}
47+
multiplier = multiplier * 26 % mod;
48+
}
49+
return s.substring(0, (int) len);
50+
}
51+
}
52+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1392;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1392Test {
10+
11+
private static _1392.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1392.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals("l", solution1.longestPrefix("level"));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals("abab", solution1.longestPrefix("ababab"));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertEquals("leet", solution1.longestPrefix("leetcodeleet"));
31+
}
32+
33+
@Test
34+
public void test4() {
35+
assertEquals("", solution1.longestPrefix("a"));
36+
}
37+
38+
@Test
39+
public void test5() {
40+
assertEquals("aaaa", solution1.longestPrefix("aaaaa"));
41+
}
42+
43+
@Test
44+
public void test6() {
45+
assertEquals("", solution1.longestPrefix("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"));
46+
}
47+
48+
}

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