Skip to content

Commit 71306b6

Browse files
committed
+ problem 2156
1 parent 20ce6a0 commit 71306b6

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 2156. Find Substring With Given Hash Value
2+
The hash of a **0-indexed** string `s` of length `k`, given integers `p` and `m`, is computed using the following function:
3+
* <code>hash(s, p, m) = (val(s[0]) * p<sup>0</sup> + val(s[1]) * p<sup>1</sup> + ... + val(s[k-1]) * p<sup>k-1</sup>) mod m</code>.
4+
5+
Where `val(s[i])` represents the index of `s[i]` in the alphabet from `val('a') = 1` to `val('z') = 26`.
6+
7+
You are given a string `s` and the integers `power`, `modulo`, `k`, and `hashValue`. Return `sub`, *the **first substring** of* `s` *of length* `k` *such that* `hash(sub, power, modulo) == hashValue`.
8+
9+
The test cases will be generated such that an answer always **exists**.
10+
11+
A **substring** is a contiguous non-empty sequence of characters within a string.
12+
13+
#### Example 1:
14+
<pre>
15+
<strong>Input:</strong> s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0
16+
<strong>Output:</strong> "ee"
17+
<strong>Explanation:</strong> The hash of "ee" can be computed to be hash("ee", 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0.
18+
"ee" is the first substring of length 2 with hashValue 0. Hence, we return "ee".
19+
</pre>
20+
21+
#### Example 2:
22+
<pre>
23+
<strong>Input:</strong> s = "fbxzaad", power = 31, modulo = 100, k = 3, hashValue = 32
24+
<strong>Output:</strong> "fbx"
25+
<strong>Explanation:</strong> The hash of "fbx" can be computed to be hash("fbx", 31, 100) = (6 * 1 + 2 * 31 + 24 * 312) mod 100 = 23132 mod 100 = 32.
26+
The hash of "bxz" can be computed to be hash("bxz", 31, 100) = (2 * 1 + 24 * 31 + 26 * 312) mod 100 = 25732 mod 100 = 32.
27+
"fbx" is the first substring of length 3 with hashValue 32. Hence, we return "fbx".
28+
Note that "bxz" also has a hash of 32 but it appears later than "fbx".
29+
</pre>
30+
31+
#### Constraints:
32+
* <code>1 <= k <= s.length <= 2 * 10<sup>4</sup></code>
33+
* <code>1 <= power, modulo <= 10<sup>9</sup></code>
34+
* `0 <= hashValue < modulo`
35+
* `s` consists of lowercase English letters only.
36+
* The test cases are generated such that an answer always **exists**.
37+
38+
## Solutions (Python)
39+
40+
### 1. Solution
41+
```Python
42+
class Solution:
43+
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
44+
def val(c): return ord(c) - 96
45+
value = 0
46+
start = 0
47+
48+
for i in range(len(s) - 1, -1, -1):
49+
value = (value * power + val(s[i])) % modulo
50+
if i + k < len(s):
51+
value = (value - val(s[i + k]) *
52+
pow(power, k, modulo)) % modulo
53+
if value == hashValue:
54+
start = i
55+
56+
return s[start:start + k]
57+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 2156. 查找给定哈希值的子串
2+
给定整数 `p``m` ,一个长度为 `k` 且下标从 **0** 开始的字符串 `s` 的哈希值按照如下函数计算:
3+
* <code>hash(s, p, m) = (val(s[0]) * p<sup>0</sup> + val(s[1]) * p<sup>1</sup> + ... + val(s[k-1]) * p<sup>k-1</sup>) mod m</code>.
4+
5+
其中 `val(s[i])` 表示 `s[i]` 在字母表中的下标,从 `val('a') = 1``val('z') = 26`
6+
7+
给你一个字符串 `s` 和整数 `power``modulo``k``hashValue` 。请你返回 `s`**第一个** 长度为 `k`**子串** `sub` ,满足 `hash(sub, power, modulo) == hashValue`
8+
9+
测试数据保证一定 **存在** 至少一个这样的子串。
10+
11+
**子串** 定义为一个字符串中连续非空字符组成的序列。
12+
13+
#### 示例 1:
14+
<pre>
15+
<strong>输入:</strong> s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0
16+
<strong>输出:</strong> "ee"
17+
<strong>解释:</strong> "ee" 的哈希值为 hash("ee", 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0 。
18+
"ee" 是长度为 2 的第一个哈希值为 0 的子串,所以我们返回 "ee" 。
19+
</pre>
20+
21+
#### 示例 2:
22+
<pre>
23+
<strong>输入:</strong> s = "fbxzaad", power = 31, modulo = 100, k = 3, hashValue = 32
24+
<strong>输出:</strong> "fbx"
25+
<strong>解释:</strong> "fbx" 的哈希值为 hash("fbx", 31, 100) = (6 * 1 + 2 * 31 + 24 * 312) mod 100 = 23132 mod 100 = 32 。
26+
"bxz" 的哈希值为 hash("bxz", 31, 100) = (2 * 1 + 24 * 31 + 26 * 312) mod 100 = 25732 mod 100 = 32 。
27+
"fbx" 是长度为 3 的第一个哈希值为 32 的子串,所以我们返回 "fbx" 。
28+
注意,"bxz" 的哈希值也为 32 ,但是它在字符串中比 "fbx" 更晚出现。
29+
</pre>
30+
31+
#### 提示:
32+
* <code>1 <= k <= s.length <= 2 * 10<sup>4</sup></code>
33+
* <code>1 <= power, modulo <= 10<sup>9</sup></code>
34+
* `0 <= hashValue < modulo`
35+
* `s` 只包含小写英文字母。
36+
* 测试数据保证一定 **存在** 满足条件的子串。
37+
38+
## 题解 (Python)
39+
40+
### 1. 题解
41+
```Python
42+
class Solution:
43+
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
44+
def val(c): return ord(c) - 96
45+
value = 0
46+
start = 0
47+
48+
for i in range(len(s) - 1, -1, -1):
49+
value = (value * power + val(s[i])) % modulo
50+
if i + k < len(s):
51+
value = (value - val(s[i + k]) *
52+
pow(power, k, modulo)) % modulo
53+
if value == hashValue:
54+
start = i
55+
56+
return s[start:start + k]
57+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
3+
def val(c): return ord(c) - 96
4+
value = 0
5+
start = 0
6+
7+
for i in range(len(s) - 1, -1, -1):
8+
value = (value * power + val(s[i])) % modulo
9+
if i + k < len(s):
10+
value = (value - val(s[i + k]) *
11+
pow(power, k, modulo)) % modulo
12+
if value == hashValue:
13+
start = i
14+
15+
return s[start:start + k]

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,7 @@
13861386
[2151][2151l]|[Maximum Good People Based on Statements][2151] |![rs]
13871387
[2154][2154l]|[Keep Multiplying Found Values by Two][2154] |![rs]
13881388
[2155][2155l]|[All Divisions With the Highest Score of a Binary Array][2155] |![rs]
1389+
[2156][2156l]|[Find Substring With Given Hash Value][2156] |![py]
13891390
[2160][2160l]|[Minimum Sum of Four Digit Number After Splitting Digits][2160] |![rs]
13901391
[2161][2161l]|[Partition Array According to Given Pivot][2161] |![py]
13911392
[2162][2162l]|[Minimum Cost to Set Cooking Time][2162] |![rs]
@@ -3056,6 +3057,7 @@
30563057
[2151]:Problemset/2151-Maximum%20Good%20People%20Based%20on%20Statements/README.md#2151-maximum-good-people-based-on-statements
30573058
[2154]:Problemset/2154-Keep%20Multiplying%20Found%20Values%20by%20Two/README.md#2154-keep-multiplying-found-values-by-two
30583059
[2155]:Problemset/2155-All%20Divisions%20With%20the%20Highest%20Score%20of%20a%20Binary%20Array/README.md#2155-all-divisions-with-the-highest-score-of-a-binary-array
3060+
[2156]:Problemset/2156-Find%20Substring%20With%20Given%20Hash%20Value/README.md#2156-find-substring-with-given-hash-value
30593061
[2160]:Problemset/2160-Minimum%20Sum%20of%20Four%20Digit%20Number%20After%20Splitting%20Digits/README.md#2160-minimum-sum-of-four-digit-number-after-splitting-digits
30603062
[2161]:Problemset/2161-Partition%20Array%20According%20to%20Given%20Pivot/README.md#2161-partition-array-according-to-given-pivot
30613063
[2162]:Problemset/2162-Minimum%20Cost%20to%20Set%20Cooking%20Time/README.md#2162-minimum-cost-to-set-cooking-time
@@ -4720,6 +4722,7 @@
47204722
[2151l]:https://leetcode.com/problems/maximum-good-people-based-on-statements/
47214723
[2154l]:https://leetcode.com/problems/keep-multiplying-found-values-by-two/
47224724
[2155l]:https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/
4725+
[2156l]:https://leetcode.com/problems/find-substring-with-given-hash-value/
47234726
[2160l]:https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/
47244727
[2161l]:https://leetcode.com/problems/partition-array-according-to-given-pivot/
47254728
[2162l]:https://leetcode.com/problems/minimum-cost-to-set-cooking-time/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,7 @@
13861386
[2151][2151l]|[基于陈述统计最多好人数][2151] |![rs]
13871387
[2154][2154l]|[将找到的值乘以 2][2154] |![rs]
13881388
[2155][2155l]|[分组得分最高的所有下标][2155] |![rs]
1389+
[2156][2156l]|[查找给定哈希值的子串][2156] |![py]
13891390
[2160][2160l]|[拆分数位后四位数字的最小和][2160] |![rs]
13901391
[2161][2161l]|[根据给定数字划分数组][2161] |![py]
13911392
[2162][2162l]|[设置时间的最少代价][2162] |![rs]
@@ -3056,6 +3057,7 @@
30563057
[2151]:Problemset/2151-Maximum%20Good%20People%20Based%20on%20Statements/README_CN.md#2151-基于陈述统计最多好人数
30573058
[2154]:Problemset/2154-Keep%20Multiplying%20Found%20Values%20by%20Two/README_CN.md#2154-将找到的值乘以-2
30583059
[2155]:Problemset/2155-All%20Divisions%20With%20the%20Highest%20Score%20of%20a%20Binary%20Array/README_CN.md#2155-分组得分最高的所有下标
3060+
[2156]:Problemset/2156-Find%20Substring%20With%20Given%20Hash%20Value/README_CN.md#2156-查找给定哈希值的子串
30593061
[2160]:Problemset/2160-Minimum%20Sum%20of%20Four%20Digit%20Number%20After%20Splitting%20Digits/README_CN.md#2160-拆分数位后四位数字的最小和
30603062
[2161]:Problemset/2161-Partition%20Array%20According%20to%20Given%20Pivot/README_CN.md#2161-根据给定数字划分数组
30613063
[2162]:Problemset/2162-Minimum%20Cost%20to%20Set%20Cooking%20Time/README_CN.md#2162-设置时间的最少代价
@@ -4720,6 +4722,7 @@
47204722
[2151l]:https://leetcode.cn/problems/maximum-good-people-based-on-statements/
47214723
[2154l]:https://leetcode.cn/problems/keep-multiplying-found-values-by-two/
47224724
[2155l]:https://leetcode.cn/problems/all-divisions-with-the-highest-score-of-a-binary-array/
4725+
[2156l]:https://leetcode.cn/problems/find-substring-with-given-hash-value/
47234726
[2160l]:https://leetcode.cn/problems/minimum-sum-of-four-digit-number-after-splitting-digits/
47244727
[2161l]:https://leetcode.cn/problems/partition-array-according-to-given-pivot/
47254728
[2162l]:https://leetcode.cn/problems/minimum-cost-to-set-cooking-time/

0 commit comments

Comments
 (0)