Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1fbf170

Browse files
committed
Add solution #2156
1 parent 8ab9d01 commit 1fbf170

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,781 LeetCode solutions in JavaScript
1+
# 1,782 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1651,6 +1651,7 @@
16511651
2151|[Maximum Good People Based on Statements](./solutions/2151-maximum-good-people-based-on-statements.js)|Hard|
16521652
2154|[Keep Multiplying Found Values by Two](./solutions/2154-keep-multiplying-found-values-by-two.js)|Easy|
16531653
2155|[All Divisions With the Highest Score of a Binary Array](./solutions/2155-all-divisions-with-the-highest-score-of-a-binary-array.js)|Medium|
1654+
2156|[Find Substring With Given Hash Value](./solutions/2156-find-substring-with-given-hash-value.js)|Hard|
16541655
2161|[Partition Array According to Given Pivot](./solutions/2161-partition-array-according-to-given-pivot.js)|Medium|
16551656
2176|[Count Equal and Divisible Pairs in an Array](./solutions/2176-count-equal-and-divisible-pairs-in-an-array.js)|Easy|
16561657
2179|[Count Good Triplets in an Array](./solutions/2179-count-good-triplets-in-an-array.js)|Hard|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 2156. Find Substring With Given Hash Value
3+
* https://leetcode.com/problems/find-substring-with-given-hash-value/
4+
* Difficulty: Hard
5+
*
6+
* The hash of a 0-indexed string s of length k, given integers p and m, is computed using
7+
* the following function:
8+
* - hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m.
9+
*
10+
* Where val(s[i]) represents the index of s[i] in the alphabet from val('a') = 1 to val('z') = 26.
11+
*
12+
* You are given a string s and the integers power, modulo, k, and hashValue. Return sub, the
13+
* first substring of s of length k such that hash(sub, power, modulo) == hashValue.
14+
*
15+
* The test cases will be generated such that an answer always exists.
16+
*
17+
* A substring is a contiguous non-empty sequence of characters within a string.
18+
*/
19+
20+
/**
21+
* @param {string} s
22+
* @param {number} power
23+
* @param {number} modulo
24+
* @param {number} k
25+
* @param {number} hashValue
26+
* @return {string}
27+
*/
28+
var subStrHash = function(s, power, modulo, k, hashValue) {
29+
power = BigInt(power);
30+
modulo = BigInt(modulo);
31+
hashValue = BigInt(hashValue);
32+
33+
const powers = new Array(k);
34+
powers[0] = 1n;
35+
for (let i = 1; i < k; i++) {
36+
powers[i] = (powers[i - 1] * power) % modulo;
37+
}
38+
39+
for (let start = 0; start <= s.length - k; start++) {
40+
let hash = 0n;
41+
for (let i = 0; i < k; i++) {
42+
const charVal = BigInt(s.charCodeAt(start + i) - 'a'.charCodeAt(0) + 1);
43+
hash = (hash + charVal * powers[i]) % modulo;
44+
}
45+
if (hash === hashValue) {
46+
return s.substring(start, start + k);
47+
}
48+
}
49+
50+
return '';
51+
};

0 commit comments

Comments
 (0)