Skip to content

Commit c6404c5

Browse files
add 2156
1 parent 0aa304f commit c6404c5

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-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+
|2156|[Find Substring With Given Hash Value](https://leetcode.com/problems/find-substring-with-given-hash-value/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2156.java) ||Medium||
1112
|2155|[All Divisions With the Highest Score of a Binary Array](https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2155.java) ||Medium||
1213
|2154|[Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2154.java) ||Easy||
1314
|2150|[Find All Lonely Numbers in the Array](https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2150.java) ||Medium||
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2156 {
4+
public static class Solution1 {
5+
/**
6+
* Credit: https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1730100/Java-rolling-hash(back-to-front)/1242659
7+
* <p>
8+
* We start from the right side and compute rolling hash when moving the window of size k towards the left.
9+
*/
10+
public String subStrHash(String s, int power, int modulo, int k, int hashValue) {
11+
long weight = 1;
12+
for (int j = 0; j < k - 1; j++) {
13+
// calculate the weight which will be the power to the k-1
14+
// this will be used when we start shifting our window of size k to the left from the end of the string
15+
weight = (weight * power) % modulo;
16+
}
17+
/**We'll have to use the above for loop to calculate weight instead of using Math.pow(power, k - 1) which will render wrong results when power and k are big enough.*/
18+
19+
// initialize the result string to empty string and keep updating it as we start from the end of the string, and we need to find the first substring that has the hashvalue
20+
String result = "";
21+
22+
// right bound of the sliding window which starts at the end of the string
23+
int right = s.length() - 1;
24+
25+
long hash = 0;
26+
for (int i = s.length() - 1; i >= 0; i--) {
27+
28+
// add the next value of char for the left pointer into the sliding window
29+
int val = s.charAt(i) - 'a' + 1;
30+
31+
// update the current hash value
32+
hash = (hash * power % modulo + val) % modulo;
33+
34+
// when window is at size k, we need to check if we find a matching hash value
35+
// and update the result, and remove the right most char out of the window to prepare for next iteration
36+
if (right - i + 1 == k) {
37+
if (hash == hashValue) {
38+
result = s.substring(i, right + 1);
39+
}
40+
hash = (hash + modulo - (s.charAt(right--) - 'a' + 1) * weight % modulo) % modulo;
41+
}
42+
}
43+
44+
return result;
45+
}
46+
47+
}
48+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2156;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2156Test {
10+
private static _2156.Solution1 solution1;
11+
private static String s;
12+
private int power;
13+
private int modulo;
14+
private static int k;
15+
private static int hashValue;
16+
private static String expected;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _2156.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
s = "leetcode";
26+
power = 7;
27+
modulo = 20;
28+
k = 2;
29+
hashValue = 0;
30+
expected = "ee";
31+
assertEquals(expected, solution1.subStrHash(s, power, modulo, k, hashValue));
32+
}
33+
34+
@Test
35+
public void test2() {
36+
s = "fbxzaad";
37+
power = 31;
38+
modulo = 100;
39+
k = 3;
40+
hashValue = 32;
41+
expected = "fbx";
42+
assertEquals(expected, solution1.subStrHash(s, power, modulo, k, hashValue));
43+
}
44+
45+
@Test
46+
public void test3() {
47+
s = "xmmhdakfursinye";
48+
power = 96;
49+
modulo = 45;
50+
k = 15;
51+
hashValue = 21;
52+
expected = "xmmhdakfursinye";
53+
System.out.println(Math.pow(power, k - 1));
54+
System.out.println(Math.pow(power, k - 1) % modulo);
55+
assertEquals(expected, solution1.subStrHash(s, power, modulo, k, hashValue));
56+
}
57+
58+
}

0 commit comments

Comments
 (0)