-
Notifications
You must be signed in to change notification settings - Fork 0
/
general_algos.h
280 lines (219 loc) · 5 KB
/
general_algos.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <string>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <vector>
namespace string
{
bool checkIfPalindrome(const std::string& str)
{
size_t middleSize = str.size() / 2;
for (size_t start = 0, end = str.size() - 1; start != middleSize && end != middleSize;
++start, --end)
{
if (str.at(start) != str.at(end))
return false;
}
return true;
}
bool ans(const std::string& a, const std::string& b, const std::string& c, int la, int lb, int lc)
{
if (lc == c.length())
return true;
bool f = false;
if (la<a.length() && a[la] == c[la + lb])
f = f || ans(a, b, c, la + 1, lb, lc + 1);
if (lb<b.length() && b[lb] == c[la + lb])
f = f || ans(a, b, c, la, lb + 1, lc + 1);
return f;
}
bool isInterleave(const std::string& A, const std::string& B, const std::string& C)
{
return ans(A, B, C, 0, 0, 0);
}
void getNumericAbreviationsWorker(const std::string& sample, int index,
int max_index, std::string st)
{
if (index == max_index)
{
std::cout << st << "\n";
return;
}
st.push_back(sample[index]);
getNumericAbreviationsWorker(sample, index + 1, max_index, st);
st.pop_back();
int count = 1;
if (!st.empty())
{
if (isdigit(st.back()))
{
count += (int)(st.back() - '0');
st.pop_back();
}
}
char to_print = (char)(count + '0');
st.push_back(to_print);
getNumericAbreviationsWorker(sample, index + 1, max_index, st);
}
// Wrapper function
void getNumericAbreviations(const std::string& example)
{
if (example.empty())
return;
std::string st;
getNumericAbreviationsWorker(example, 0, example.length(), st);
}
//k is a number of unique symbols
std::string findLongestSubStr(const std::string& strSource, int k)
{
std::unordered_set<char> uniqueSymbols;
std::string longest;
longest.reserve(strSource.size());
std::string current;
size_t currentUnique = 0;
for (char symbol : strSource)
{ // there is a bug, it is incorrect
if (uniqueSymbols.find(symbol) == std::end(uniqueSymbols))
{
++currentUnique;
}
if (currentUnique > k)
{
if (current.size() >= longest.size())
longest = current;
current.clear();
currentUnique = 0;
uniqueSymbols.clear();
}
else
{
current += symbol;
uniqueSymbols.insert(symbol);
}
}
if (current.size() > longest.size())
longest = current;
return longest;
}
}
namespace bits
{
std::set<std::pair<int, int>> generatePairs(const std::vector<int>& inputNums)
{
std::set<std::pair<int, int>> pairs;
std::set<int> explroredNums;
for (size_t i = 0; i < inputNums.size(); ++i)
for (size_t j = i + 1; j < inputNums.size(); ++j)
{
pairs.insert(std::make_pair(inputNums[i], inputNums[j]));
pairs.insert(std::make_pair(inputNums[j], inputNums[i]));
}
return pairs;
}
int countOneBits(int number)
{
int counter = 0;
while (number)
{
if (number % 2)
++counter;
number = number / 2;
}
return counter;
}
int diffBitSumsNaive(const std::vector<int>& inputNums)
{
if (inputNums.size() <= 1)
return 0;
std::set<std::pair<int, int>> pairs = generatePairs(inputNums);
int totalSum = 0;
for (const auto pair : pairs)
{
int xorRes = pair.first ^ pair.second;
int currentBits = countOneBits(xorRes);
totalSum += currentBits;
}
return totalSum;
}
int diffBitSumsLinear(const std::vector<int>& inputNums)
{
int ans = 0;
for (int i = 0; i < 32; i++)
{
int count = 0;
for (size_t j = 0; j < inputNums.size(); ++j)
if ((inputNums[j] & (1 << i)))
count++;
ans += (count * (inputNums.size() - count) * 2);
}
return ans;
}
}
namespace tree
{
struct treeNode
{
int value;
treeNode* left;
treeNode* right;
};
treeNode* createNode(int value)
{
treeNode* node = new treeNode;
node->value = value;
node->left = nullptr;
node->right = nullptr;
return node;
}
void printTree(treeNode* head)
{
if (!head)
return;
std::cout << "Value=" << head->value << " ";
printTree(head->left);
printTree(head->right);
}
int findDepth(treeNode* head)
{
if (!head)
return 0;
int left = findDepth(head->left);
int right = findDepth(head->right);
return (std::max(left, right) + 1);
}
bool checkIfBST(treeNode* head)
{
if (!head)
return false;
if (head->left)
{
if (head->left->value > head->value)
return false;
return checkIfBST(head->left);
}
if (head->right)
{
if (head->right->value < head->value)
return false;
return checkIfBST(head->right);
}
return true;
}
int countNodesWithSum(treeNode* node, int sum, int& count)
{
if (node)
return 0;
int res = countNodesWithSum(node->left, sum, count) + countNodesWithSum(node->right, sum, count) + node->value;
if (res == sum)
++count;
return count;
}
/* This functions prints the subnode with given sum*/
void findNodesWithSum(treeNode* node, int sum)
{
int count = 0;
std::cout << countNodesWithSum(node, sum, count) << std::endl;
}
}