Skip to content

Commit b2ac1a6

Browse files
committed
Feb-20
1 parent d112e41 commit b2ac1a6

6 files changed

+281
-7
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5353
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5454
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
5555
| [🔲] | [9c-top.md](./list/9c-top.md) | 41/56 | 2 vips |
56-
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 20/2207 | 2 vips |
56+
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 22/2207 | 2 vips |
5757

58-
**Solved**: 536 problems
58+
**Solved**: 538 problems
5959

6060
## 类型/Category
6161

@@ -436,7 +436,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
436436

437437
## Hash Table
438438

439-
| Link | Problem(76) | Solution | Tag | Time | Space | Ref |
439+
| Link | Problem(78) | Solution | Tag | Time | Space | Ref |
440440
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
441441
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Hash Table | O\(MlogM\) | O\(N\) | - |
442442
| [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\) | - |
@@ -454,7 +454,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
454454
| [Leetcode-535](https://leetcode.com/problems/encode-and-decode-tinyurl/) | Encode And Decode Tinyurl | [c++](./leetcode/535.encode-and-decode-tinyurl.cpp), [python3](./leetcode/535.encode-and-decode-tinyurl.py) | Hash Table | O\(1\) | O\(N\) | - |
455455
| [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\) | - |
456456
| [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\) | - |
457458
| [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\) | - |
459+
| [Leetcode-1980](https://leetcode.com/problems/find-unique-binary-string/) | Find Unique Binary String | [c++](./leetcode/1980.find-unique-binary-string.cpp), [python3](./leetcode/1980.find-unique-binary-string.py) | Hash Table | O\(N\) | O\(1\) | - |
458460
| [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\) | - |
459461
| [Leetcode-49](https://leetcode.com/problems/group-anagrams/) | Group Anagrams | [c++](./leetcode/49.group-anagrams.cpp), [python3](./leetcode/49.group-anagrams.py) | Hash Table | O\(NL\) | O\(N\) | - |
460462
| [Leetcode-846](https://leetcode.com/problems/hand-of-straights/) | Hand Of Straights | [c++](./leetcode/846.hand-of-straights.cpp), [python3](./leetcode/846.hand-of-straights.py) | Hash Table | O\(NlogN\) | O\(N\) | - |
@@ -647,11 +649,12 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
647649

648650
## Sliding Window
649651

650-
| Link | Problem(31) | Solution | Tag | Time | Space | Ref |
652+
| Link | Problem(32) | Solution | Tag | Time | Space | Ref |
651653
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
652654
| [Leetcode-1652](https://leetcode.com/problems/defuse-the-bomb/) | Defuse The Bomb | [c++](./leetcode/1652.defuse-the-bomb.cpp), [python3](./leetcode/1652.defuse-the-bomb.py) | Sliding Window | O\(N\) | O\(1\) | - |
653655
| [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) | Sliding Window | O\(N\) | O\(K\) | - |
654656
| [Leetcode-658](https://leetcode.com/problems/find-k-closest-elements/) | Find K Closest Elements | [c++](./leetcode/658.find-k-closest-elements.cpp), [python3](./leetcode/658.find-k-closest-elements.py) | Sliding Window | O\(Log\(N \- K\)\) | O\(1\) | - |
657+
| [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) | Sliding Window | O\(N\) | O\(1\) | - |
655658
| [Leetcode-1052](https://leetcode.com/problems/grumpy-bookstore-owner/) | Grumpy Bookstore Owner | [c++](./leetcode/1052.grumpy-bookstore-owner.cpp), [python3](./leetcode/1052.grumpy-bookstore-owner.py) | Sliding Window | O\(N\) | O\(1\) | - |
656659
| [Leetcode-2090](https://leetcode.com/problems/k-radius-subarray-averages/) | K Radius Subarray Averages | [c++](./leetcode/2090.k-radius-subarray-averages.cpp), [python3](./leetcode/2090.k-radius-subarray-averages.py) | Sliding Window | O\(N\) | O\(1\) | - |
657660
| [Leetcode-594](https://leetcode.com/problems/longest-harmonious-subsequence/) | Longest Harmonious Subsequence | [c++](./leetcode/594.longest-harmonious-subsequence.cpp), [python3](./leetcode/594.longest-harmonious-subsequence.py) | Sliding Window | O\(N\) | O\(N\) | - |
@@ -907,14 +910,15 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
907910

908911
## Backtracking
909912

910-
| Link | Problem(19) | Solution | Tag | Time | Space | Ref |
913+
| Link | Problem(20) | Solution | Tag | Time | Space | Ref |
911914
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
912915
| [Leetcode-257](https://leetcode.com/problems/binary-tree-paths/) | Binary Tree Paths | [c++](./leetcode/257.binary-tree-paths.cpp), [python3](./leetcode/257.binary-tree-paths.py) | Backtracking | O\(N\) | O\(H\) | - |
913916
| [Leetcode-40](https://leetcode.com/problems/combination-sum-ii/) | Combination Sum II | [c++](./leetcode/40.combination-sum-ii.cpp), [python3](./leetcode/40.combination-sum-ii.py) | Backtracking | O\(2^N\) | O\(N\) | - |
914917
| [Leetcode-77](https://leetcode.com/problems/combinations/) | Combinations | [c++](./leetcode/77.combinations.cpp), [python3](./leetcode/77.combinations.py) | Backtracking | O\(k \* C\(n, k\)\) | O\(K\) | - |
915918
| [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\) | - |
916919
| [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\) | - |
917920
| [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\) | - |
921+
| [Leetcode-1980](https://leetcode.com/problems/find-unique-binary-string/) | Find Unique Binary String | [c++](./leetcode/1980.find-unique-binary-string.cpp), [python3](./leetcode/1980.find-unique-binary-string.py) | Backtracking | O\(N\) | O\(1\) | - |
918922
| [Leetcode-22](https://leetcode.com/problems/generate-parentheses/) | Generate Parentheses | [c++](./leetcode/22.generate-parentheses.cpp), [python3](./leetcode/22.generate-parentheses.py) | Backtracking | O\(2^N\) | O\(N\) | - |
919923
| [Leetcode-1079](https://leetcode.com/problems/letter-tile-possibilities/) | Letter Tile Possibilities | [c++](./leetcode/1079.letter-tile-possibilities.cpp), [python3](./leetcode/1079.letter-tile-possibilities.py) | Backtracking | O\(N\!\) | O\(N\) | - |
920924
| [Leetcode-51](https://leetcode.com/problems/n-queens/) | N Queens | [c++](./leetcode/51.n-queens.cpp), [python3](./leetcode/51.n-queens.py) | Backtracking | O\(N\!\) | O\(N\) | - |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Tag: Array, Hash Table, String, Backtracking
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/B7Nl-Lb76JU
7+
8+
// 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.
27+
//
28+
//  
29+
// Constraints:
30+
//
31+
// n == nums.length
32+
// 1 <= n <= 16
33+
// nums[i].length == n
34+
// nums[i] is either '0' or '1'.
35+
// All the strings of nums are unique.
36+
//
37+
//
38+
39+
class Solution {
40+
public:
41+
string findDifferentBinaryString(vector<string>& nums) {
42+
int n = nums.size();
43+
string res;
44+
for (int i = 0; i < n; i++) {
45+
res += nums[i][i] == '0' ? '1': '0';
46+
}
47+
return res;
48+
}
49+
};
50+
51+
class Solution {
52+
public:
53+
string findDifferentBinaryString(vector<string>& nums) {
54+
unordered_set<string> table(nums.begin(), nums.end());
55+
int n = nums[0].size();
56+
string res;
57+
helper(table, res, 0, n);
58+
return res;
59+
}
60+
61+
bool helper(unordered_set<string>& table, string& res, int i, int n) {
62+
if (i == n) {
63+
return table.count(res) == 0;
64+
}
65+
66+
for (char ch : {'0', '1'}) {
67+
res += ch;
68+
if (helper(table, res, i + 1, n)) {
69+
return true;
70+
}
71+
res.pop_back();
72+
}
73+
74+
return false;
75+
}
76+
77+
};
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Tag: Array, Hash Table, String, Backtracking
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/B7Nl-Lb76JU
7+
8+
# 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.
27+
#
28+
#  
29+
# Constraints:
30+
#
31+
# n == nums.length
32+
# 1 <= n <= 16
33+
# nums[i].length == n
34+
# nums[i] is either '0' or '1'.
35+
# All the strings of nums are unique.
36+
#
37+
#
38+
39+
class Solution(object):
40+
def findDifferentBinaryString(self, nums):
41+
result = ""
42+
for i in range(len(nums)):
43+
result += '1' if nums[i][i] == '0' else '0'
44+
45+
return result
46+
47+
class Solution:
48+
def findDifferentBinaryString(self, nums: List[str]) -> str:
49+
table = set(nums)
50+
n = len(nums[0])
51+
self.res = None
52+
self.helper(table, '', 0, n)
53+
return self.res
54+
55+
def helper(self, table: set, tmp: str, i: int, n: int) -> bool:
56+
if i == n:
57+
self.res = tmp
58+
return tmp not in table
59+
60+
for ch in '01':
61+
if self.helper(table, tmp + ch, i + 1, n):
62+
return True
63+
64+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// 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+
class Solution {
44+
public:
45+
string subStrHash(string s, int power, int modulo, int k, int hashValue) {
46+
int n = s.size();
47+
long long hash = 0;
48+
long long pk = 1;
49+
int index = 0;
50+
for (int i = n - 1; i >= 0; i--) {
51+
hash = (hash * power + val(s[i])) % modulo;
52+
if (i < n - k) {
53+
hash = (hash - val(s[i + k]) * pk % modulo + modulo) % modulo;
54+
} else {
55+
pk = pk * power % modulo;
56+
}
57+
58+
if (hash == hashValue) {
59+
index = i;
60+
}
61+
}
62+
63+
return s.substr(index, k);
64+
}
65+
66+
int val(char ch) {
67+
return ch - 'a' + 1;
68+
}
69+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 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+
class Solution:
44+
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
45+
n = len(s)
46+
hash = 0
47+
pk = pow(power, k, modulo)
48+
index = 0
49+
for i in range(n - 1, -1, -1):
50+
hash = (hash * power + self.val(s[i])) % modulo
51+
if i < n - k:
52+
hash = (hash - self.val(s[i + k]) * pk + modulo) % modulo
53+
54+
if hash == hashValue:
55+
index = i
56+
57+
return s[index: index + k]
58+
59+
def val(self, ch: str):
60+
return ord(ch) - ord('a') + 1

list/灵茶山艾府.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
20. https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/
2828
21. https://leetcode.com/problems/permutation-in-string/
2929
22. https://leetcode.com/problems/find-all-anagrams-in-a-string/
30-
- https://leetcode.com/problems/substring-with-concatenation-of-all-words/
31-
- https://leetcode.com/problems/find-substring-with-given-hash-value/
30+
23. https://leetcode.com/problems/substring-with-concatenation-of-all-words/
31+
24. https://leetcode.com/problems/find-substring-with-given-hash-value/
3232
- https://leetcode.com/problems/count-complete-substrings/
3333
- https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/
3434
- https://leetcode.com/problems/k-empty-slots/

0 commit comments

Comments
 (0)