You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/)| All Nodes Distance K In Binary Tree |[c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py)| Hash Table | O\(N\)| O\(N\)| - |
|[Leetcode-438](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| Find All Anagrams In A String |[c++](./leetcode/438.find-all-anagrams-in-a-string.cpp), [python3](./leetcode/438.find-all-anagrams-in-a-string.py)| Hash Table | O\(N\)| O\(K\)| - |
456
456
|[Leetcode-448](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)| Find All Numbers Disappeared In An Array |[c++](./leetcode/448.find-all-numbers-disappeared-in-an-array.cpp), [python3](./leetcode/448.find-all-numbers-disappeared-in-an-array.py)| Hash Table | O\(N\)| O\(1\)| - |
457
+
|[Leetcode-2156](https://leetcode.com/problems/find-substring-with-given-hash-value/)| Find Substring With Given Hash Value |[c++](./leetcode/2156.find-substring-with-given-hash-value.cpp), [python3](./leetcode/2156.find-substring-with-given-hash-value.py)| Hash Table | O\(N\)| O\(1\)| - |
457
458
|[Leetcode-3160](https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/)| Find The Number Of Distinct Colors Among The Balls |[c++](./leetcode/3160.find-the-number-of-distinct-colors-among-the-balls.cpp), [python3](./leetcode/3160.find-the-number-of-distinct-colors-among-the-balls.py)| Hash Table | O\(N\)| O\(N\)| - |
|[Leetcode-387](https://leetcode.com/problems/first-unique-character-in-a-string/)| First Unique Character In A String |[c++](./leetcode/387.first-unique-character-in-a-string.cpp), [python3](./leetcode/387.first-unique-character-in-a-string.py)| Hash Table | O\(N\)| O\(N\)| - |
|[Leetcode-2375](https://leetcode.com/problems/construct-smallest-number-from-di-string/)| Construct Smallest Number From Di String |[c++](./leetcode/2375.construct-smallest-number-from-di-string.cpp), [python3](./leetcode/2375.construct-smallest-number-from-di-string.py)| Backtracking | O\(N\)| O\(N\)| - |
916
919
|[Leetcode-1718](https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/)| Construct The Lexicographically Largest Valid Sequence |[c++](./leetcode/1718.construct-the-lexicographically-largest-valid-sequence.cpp), [python3](./leetcode/1718.construct-the-lexicographically-largest-valid-sequence.py)| Backtracking | O\(N\!\)| O\(N\)| - |
917
920
|[Leetcode-2698](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/)| Find The Punishment Number Of An Integer |[c++](./leetcode/2698.find-the-punishment-number-of-an-integer.cpp), [python3](./leetcode/2698.find-the-punishment-number-of-an-integer.py)| Backtracking | O\(N \* 2^K\)| O\(K\)| - |
// Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them.
9
+
//
10
+
// Example 1:
11
+
//
12
+
// Input: nums = ["01","10"]
13
+
// Output: "11"
14
+
// Explanation: "11" does not appear in nums. "00" would also be correct.
15
+
//
16
+
// Example 2:
17
+
//
18
+
// Input: nums = ["00","01"]
19
+
// Output: "11"
20
+
// Explanation: "11" does not appear in nums. "10" would also be correct.
21
+
//
22
+
// Example 3:
23
+
//
24
+
// Input: nums = ["111","011","001"]
25
+
// Output: "101"
26
+
// Explanation: "101" does not appear in nums. "000", "010", "100", and "110" would also be correct.
# Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them.
9
+
#
10
+
# Example 1:
11
+
#
12
+
# Input: nums = ["01","10"]
13
+
# Output: "11"
14
+
# Explanation: "11" does not appear in nums. "00" would also be correct.
15
+
#
16
+
# Example 2:
17
+
#
18
+
# Input: nums = ["00","01"]
19
+
# Output: "11"
20
+
# Explanation: "11" does not appear in nums. "10" would also be correct.
21
+
#
22
+
# Example 3:
23
+
#
24
+
# Input: nums = ["111","011","001"]
25
+
# Output: "101"
26
+
# Explanation: "101" does not appear in nums. "000", "010", "100", and "110" would also be correct.
// Tag: String, Sliding Window, Rolling Hash, Hash Function
2
+
// Time: O(N)
3
+
// Space: O(1)
4
+
// Ref: -
5
+
// Note: -
6
+
7
+
// The hash of a 0-indexed string s of length k, given integers p and m, is computed using the following function:
8
+
//
9
+
// hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m.
10
+
//
11
+
// Where val(s[i]) represents the index of s[i] in the alphabet from val('a') = 1 to val('z') = 26.
12
+
// 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.
13
+
// The test cases will be generated such that an answer always exists.
14
+
// A substring is a contiguous non-empty sequence of characters within a string.
15
+
//
16
+
// Example 1:
17
+
//
18
+
// Input: s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0
19
+
// Output: "ee"
20
+
// Explanation: The hash of "ee" can be computed to be hash("ee", 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0.
21
+
// "ee" is the first substring of length 2 with hashValue 0. Hence, we return "ee".
22
+
//
23
+
// Example 2:
24
+
//
25
+
// Input: s = "fbxzaad", power = 31, modulo = 100, k = 3, hashValue = 32
26
+
// Output: "fbx"
27
+
// Explanation: 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.
28
+
// 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.
29
+
// "fbx" is the first substring of length 3 with hashValue 32. Hence, we return "fbx".
30
+
// Note that "bxz" also has a hash of 32 but it appears later than "fbx".
31
+
//
32
+
//
33
+
// Constraints:
34
+
//
35
+
// 1 <= k <= s.length <= 2 * 104
36
+
// 1 <= power, modulo <= 109
37
+
// 0 <= hashValue < modulo
38
+
// s consists of lowercase English letters only.
39
+
// The test cases are generated such that an answer always exists.
40
+
//
41
+
//
42
+
43
+
classSolution {
44
+
public:
45
+
string subStrHash(string s, int power, int modulo, int k, int hashValue) {
# Tag: String, Sliding Window, Rolling Hash, Hash Function
2
+
# Time: O(N)
3
+
# Space: O(1)
4
+
# Ref: -
5
+
# Note: -
6
+
7
+
# The hash of a 0-indexed string s of length k, given integers p and m, is computed using the following function:
8
+
#
9
+
# hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m.
10
+
#
11
+
# Where val(s[i]) represents the index of s[i] in the alphabet from val('a') = 1 to val('z') = 26.
12
+
# 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.
13
+
# The test cases will be generated such that an answer always exists.
14
+
# A substring is a contiguous non-empty sequence of characters within a string.
15
+
#
16
+
# Example 1:
17
+
#
18
+
# Input: s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0
19
+
# Output: "ee"
20
+
# Explanation: The hash of "ee" can be computed to be hash("ee", 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0.
21
+
# "ee" is the first substring of length 2 with hashValue 0. Hence, we return "ee".
22
+
#
23
+
# Example 2:
24
+
#
25
+
# Input: s = "fbxzaad", power = 31, modulo = 100, k = 3, hashValue = 32
26
+
# Output: "fbx"
27
+
# Explanation: 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.
28
+
# 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.
29
+
# "fbx" is the first substring of length 3 with hashValue 32. Hence, we return "fbx".
30
+
# Note that "bxz" also has a hash of 32 but it appears later than "fbx".
31
+
#
32
+
#
33
+
# Constraints:
34
+
#
35
+
# 1 <= k <= s.length <= 2 * 104
36
+
# 1 <= power, modulo <= 109
37
+
# 0 <= hashValue < modulo
38
+
# s consists of lowercase English letters only.
39
+
# The test cases are generated such that an answer always exists.
0 commit comments