Skip to content

Commit dd7c4a8

Browse files
committed
Feb-21
1 parent b2ac1a6 commit dd7c4a8

8 files changed

+529
-12
lines changed

README.md

+15-8
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Tag: String
2+
// Time: O(|S|)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given a binary string s and a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise.
8+
// A substring is a contiguous sequence of characters within a string.
9+
//  
10+
// Example 1:
11+
// Input: s = "0110", n = 3
12+
// Output: true
13+
// Example 2:
14+
// Input: s = "0110", n = 4
15+
// Output: false
16+
//
17+
//  
18+
// Constraints:
19+
//
20+
// 1 <= s.length <= 1000
21+
// s[i] is either '0' or '1'.
22+
// 1 <= n <= 109
23+
//
24+
//
25+
26+
class Solution {
27+
public:
28+
bool queryString(string s, int n) {
29+
int bits = ceil(log2(n + 1));
30+
unordered_set<int> table;
31+
int mask = 0;
32+
33+
for (int k = 1; k <= bits; k++) { // 32 times most
34+
mask = (mask << 1) + 1;
35+
long long num = 0;
36+
for (int i = 0; i < s.size(); i++) {
37+
num = ((num << 1) + s[i] - '0') & mask;
38+
if (i >= k - 1) {
39+
if (num <= n && num > 0) {
40+
table.insert(num);
41+
}
42+
}
43+
}
44+
}
45+
46+
return table.size() == n;
47+
}
48+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Tag: String
2+
# Time: O(|S|)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given a binary string s and a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise.
8+
# A substring is a contiguous sequence of characters within a string.
9+
#  
10+
# Example 1:
11+
# Input: s = "0110", n = 3
12+
# Output: true
13+
# Example 2:
14+
# Input: s = "0110", n = 4
15+
# Output: false
16+
#
17+
#  
18+
# Constraints:
19+
#
20+
# 1 <= s.length <= 1000
21+
# s[i] is either '0' or '1'.
22+
# 1 <= n <= 109
23+
#
24+
#
25+
26+
import math
27+
class Solution:
28+
def queryString(self, s: str, n: int) -> bool:
29+
bits = math.ceil(math.log2(n + 1))
30+
table = set()
31+
mask = 0
32+
for k in range(1, bits + 1): # 32 times most
33+
mask = (mask << 1) + 1
34+
num = 0
35+
for i in range(len(s)):
36+
num = ((num << 1) + int(s[i])) & mask
37+
if i >= k - 1:
38+
if 0 <= num <= n:
39+
table.add(num)
40+
41+
return len(table) == n
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Tag: Hash Table, Tree, Depth-First Search, Breadth-First Search, Design, Binary Tree
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/1Npf6eFaYRQ
7+
8+
// Given a binary tree with the following rules:
9+
//
10+
// root.val == 0
11+
// For any treeNode:
12+
//
13+
// If treeNode.val has a value x and treeNode.left != null, then treeNode.left.val == 2 * x + 1
14+
// If treeNode.val has a value x and treeNode.right != null, then treeNode.right.val == 2 * x + 2
15+
//
16+
//
17+
//
18+
// Now the binary tree is contaminated, which means all treeNode.val have been changed to -1.
19+
// Implement the FindElements class:
20+
//
21+
// FindElements(TreeNode* root) Initializes the object with a contaminated binary tree and recovers it.
22+
// bool find(int target) Returns true if the target value exists in the recovered binary tree.
23+
//
24+
//  
25+
// Example 1:
26+
//
27+
//
28+
// Input
29+
// ["FindElements","find","find"]
30+
// [[[-1,null,-1]],[1],[2]]
31+
// Output
32+
// [null,false,true]
33+
// Explanation
34+
// FindElements findElements = new FindElements([-1,null,-1]);
35+
// findElements.find(1); // return False
36+
// findElements.find(2); // return True
37+
// Example 2:
38+
//
39+
//
40+
// Input
41+
// ["FindElements","find","find","find"]
42+
// [[[-1,-1,-1,-1,-1]],[1],[3],[5]]
43+
// Output
44+
// [null,true,true,false]
45+
// Explanation
46+
// FindElements findElements = new FindElements([-1,-1,-1,-1,-1]);
47+
// findElements.find(1); // return True
48+
// findElements.find(3); // return True
49+
// findElements.find(5); // return False
50+
// Example 3:
51+
//
52+
//
53+
// Input
54+
// ["FindElements","find","find","find","find"]
55+
// [[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]]
56+
// Output
57+
// [null,true,false,false,true]
58+
// Explanation
59+
// FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]);
60+
// findElements.find(2); // return True
61+
// findElements.find(3); // return False
62+
// findElements.find(4); // return False
63+
// findElements.find(5); // return True
64+
//
65+
//  
66+
// Constraints:
67+
//
68+
// TreeNode.val == -1
69+
// The height of the binary tree is less than or equal to 20
70+
// The total number of nodes is between [1, 104]
71+
// Total calls of find() is between [1, 104]
72+
// 0 <= target <= 106
73+
//
74+
//
75+
76+
/**
77+
* Definition for a binary tree node.
78+
* struct TreeNode {
79+
* int val;
80+
* TreeNode *left;
81+
* TreeNode *right;
82+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
83+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
84+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
85+
* };
86+
*/
87+
class FindElements {
88+
public:
89+
unordered_set<int> visited;
90+
FindElements(TreeNode* root) {
91+
root->val = 0;
92+
queue<TreeNode*> q;
93+
q.push(root);
94+
while (!q.empty()) {
95+
TreeNode *cur = q.front();
96+
q.pop();
97+
visited.insert(cur->val);
98+
if (cur->left) {
99+
cur->left->val = cur->val * 2 + 1;
100+
q.push(cur->left);
101+
}
102+
103+
if (cur->right) {
104+
cur->right->val = cur->val * 2 + 2;
105+
q.push(cur->right);
106+
}
107+
}
108+
}
109+
110+
bool find(int target) {
111+
return visited.count(target) > 0;
112+
}
113+
};
114+
115+
class FindElements {
116+
public:
117+
TreeNode* root;
118+
FindElements(TreeNode* root) : root(root) {}
119+
120+
bool find(int target) {
121+
int path = target + 1;
122+
TreeNode* node = root;
123+
int shift = 31;
124+
while (!(path & (1 << shift))) { // Skip leading zeros
125+
shift -= 1;
126+
continue;
127+
}
128+
shift -= 1; // Skip leading one
129+
130+
while (shift >= 0) {
131+
if (!node) return false;
132+
node = (path & (1 << shift)) ? node->right : node->left;
133+
shift -= 1;
134+
}
135+
136+
return node != nullptr;
137+
}
138+
};
139+
140+
/**
141+
* Your FindElements object will be instantiated and called as such:
142+
* FindElements* obj = new FindElements(root);
143+
* bool param_1 = obj->find(target);
144+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Tag: Hash Table, Tree, Depth-First Search, Breadth-First Search, Design, Binary Tree
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/1Npf6eFaYRQ
7+
8+
# Given a binary tree with the following rules:
9+
#
10+
# root.val == 0
11+
# For any treeNode:
12+
#
13+
# If treeNode.val has a value x and treeNode.left != null, then treeNode.left.val == 2 * x + 1
14+
# If treeNode.val has a value x and treeNode.right != null, then treeNode.right.val == 2 * x + 2
15+
#
16+
#
17+
#
18+
# Now the binary tree is contaminated, which means all treeNode.val have been changed to -1.
19+
# Implement the FindElements class:
20+
#
21+
# FindElements(TreeNode* root) Initializes the object with a contaminated binary tree and recovers it.
22+
# bool find(int target) Returns true if the target value exists in the recovered binary tree.
23+
#
24+
#  
25+
# Example 1:
26+
#
27+
#
28+
# Input
29+
# ["FindElements","find","find"]
30+
# [[[-1,null,-1]],[1],[2]]
31+
# Output
32+
# [null,false,true]
33+
# Explanation
34+
# FindElements findElements = new FindElements([-1,null,-1]);
35+
# findElements.find(1); // return False
36+
# findElements.find(2); // return True
37+
# Example 2:
38+
#
39+
#
40+
# Input
41+
# ["FindElements","find","find","find"]
42+
# [[[-1,-1,-1,-1,-1]],[1],[3],[5]]
43+
# Output
44+
# [null,true,true,false]
45+
# Explanation
46+
# FindElements findElements = new FindElements([-1,-1,-1,-1,-1]);
47+
# findElements.find(1); // return True
48+
# findElements.find(3); // return True
49+
# findElements.find(5); // return False
50+
# Example 3:
51+
#
52+
#
53+
# Input
54+
# ["FindElements","find","find","find","find"]
55+
# [[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]]
56+
# Output
57+
# [null,true,false,false,true]
58+
# Explanation
59+
# FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]);
60+
# findElements.find(2); // return True
61+
# findElements.find(3); // return False
62+
# findElements.find(4); // return False
63+
# findElements.find(5); // return True
64+
#
65+
#  
66+
# Constraints:
67+
#
68+
# TreeNode.val == -1
69+
# The height of the binary tree is less than or equal to 20
70+
# The total number of nodes is between [1, 104]
71+
# Total calls of find() is between [1, 104]
72+
# 0 <= target <= 106
73+
#
74+
#
75+
76+
# Definition for a binary tree node.
77+
# class TreeNode:
78+
# def __init__(self, val=0, left=None, right=None):
79+
# self.val = val
80+
# self.left = left
81+
# self.right = right
82+
from collections import deque
83+
class FindElements:
84+
85+
def __init__(self, root: Optional[TreeNode]):
86+
root.val = 0
87+
q = deque([root])
88+
self.visited = set()
89+
while len(q) > 0:
90+
cur = q.popleft()
91+
self.visited.add(cur.val)
92+
if cur.left:
93+
cur.left.val = cur.val * 2 + 1
94+
q.append(cur.left)
95+
96+
if cur.right:
97+
cur.right.val = cur.val * 2 + 2
98+
q.append(cur.right)
99+
100+
def find(self, target: int) -> bool:
101+
return target in self.visited
102+
103+
class FindElements:
104+
105+
def __init__(self, root: Optional[TreeNode]):
106+
self.root = root
107+
108+
def find(self, target: int) -> bool:
109+
node = self.root
110+
bin_str = bin(target+1)[3:]
111+
for bit in bin_str:
112+
if node is not None:
113+
node = (node.left, node.right)[int(bit)]
114+
else:
115+
return False
116+
return bool(node)
117+
118+
# Your FindElements object will be instantiated and called as such:
119+
# obj = FindElements(root)
120+
# param_1 = obj.find(target)

0 commit comments

Comments
 (0)