-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.cpp
1297 lines (1142 loc) · 34.2 KB
/
Test.cpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<string>
#include <set>
#include<vector>
#include<iostream>
#include<map>
#include<unordered_map>
#include<algorithm>
#include<stack>
#include<unordered_set>
#include <iterator>
#include<queue>
using namespace std;
void dfsHelper(vector<vector<int>>& res, vector<int>& path, unordered_map<int, int>& numMap, int len)
{
if (path.size() == len) { res.push_back(path); return; }
for (auto it = numMap.begin(); it != numMap.end(); ++it)
{
if (it->second)
{
path.push_back(it->first); // update the current path
--(it->second); // and map
dfsHelper(res, path, numMap, len); // then do dfs at the next level
path.pop_back(); // backtracking by recovering path and map
++(it->second);
}
}
}
void dfs(vector<vector<int>>& res, vector<int>& cur, vector<int> canVec, int len)
{
if (cur.size() == len)
{
res.push_back(cur);
}
else
{
for (auto i = 0; i < canVec.size(); ++i)
{
if (i > 0 && canVec[i] == canVec[i - 1]) continue;
cur.push_back(canVec[i]);
vector<int> temp = canVec;
temp.erase(temp.begin() + i);
dfs(res, cur, temp, len);
cur.pop_back();
}
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
//int i, len = nums.size();
//vector<vector<int>> res;
//if (len > 0)
//{
// vector<int> path;
// unordered_map<int, int> numMap; //generate a map
// for (i = 0; i < len; ++i) ++numMap[nums[i]];
// dfsHelper(res, path, numMap, len);
//}
//return res;
vector<vector<int>> res;
int len = nums.size();
if (len > 0)
{
vector<int> cur;
std::sort(nums.begin(), nums.end());
dfs(res, cur, nums, len);
}
return res;
}
void plantFlowerInGarden(int G, vector<int> adjG, vector<int>& result)
{
bool found;
for (int f = 1; f <= 4; ++f)
{
found = true;
for (int n : adjG)
{
if (result[n] != -1 && result[n] == f)
found = false;
}
if (found)
{
result[G] = f;
return;
}
}
}
vector<vector<int>> graph;
vector<int> flowers;
vector<int> gardenNoAdj1(int N, vector<vector<int>>& paths) {
vector<int> result(N, -1);
//graph(N);
for (auto e : paths)
{
graph[e[0] - 1].push_back(e[1] - 1);
graph[e[1] - 1].push_back(e[0] - 1);
}
for (int i = 0; i < N; ++i)
plantFlowerInGarden(i, graph[i], result);
return result;
}
bool used(int i, int flower) {
for (auto& neighbor : graph[i])
if (flowers[neighbor - 1] == flower)
return true;
return false;
}
void dfs1(int i) {
if (flowers[i - 1])
return; // already visited
int f = 1;
while (used(i, f))
f++;
flowers[i - 1] = f;
for (auto& neighbor : graph[i]) {
dfs1(neighbor);
}
}
vector<int> gardenNoAdj(int N, vector<vector<int>>& paths) {
graph.resize(N + 1);
flowers.resize(N);
for (auto& path : paths) {
graph[path[0]].push_back(path[1]);
graph[path[1]].push_back(path[0]);
}
for (int i = 1; i <= N; ++i) {
dfs1(i);
}
return flowers;
}
vector<string> letterCombinations(string digits)
{
string alphabet[] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
vector<string> result;
if (digits.empty()) return result;
result.push_back("");
for (int i = 0; i < digits.length(); ++i)
{
int val = digits[i] - '0';
if (val > 0 || val < 9)
{
string sequnece = alphabet[val];
if (sequnece.empty()) return result;
vector<string> temp;
for (int i = 0; i < sequnece.length(); ++i)
{
for (int j = 0; j < result.size(); ++j)
{
temp.push_back(result[j] + sequnece[i]);
}
}
result.swap(temp);
}
}
return result;
}
//BFS
vector<string> letterCombinations2(string digits) {
if (digits.empty()) {
return vector<string>();
}
vector<string> words{ " ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
deque<string> q;
q.push_back("");
for (char digit : digits) {
int d = digit - '0';
for (int i = q.size(); i > 0; i--) {
string token = q.front();
q.pop_front();
for (char ch : words[d]) {
q.push_back(token + ch);
}
}
}
return vector<string>(q.begin(), q.end());
}
/**
* start with an empty builder, for every digit, use all chars it represents to attach to the builder, when i reaches the end of digits, push builder to result;
*/
void build(string builder, int i, const string& digits, const vector<string>& chars, vector<string>& combs) {
if (i == digits.size()) {
combs.push_back(builder);
return;
}
int d = digits[i] - '0';
for (char ch : chars[d]) {
build(builder + ch, i + 1, digits, chars, combs);
}
}
vector<string> letterCombinations3(string digits) {
if (!digits.size()) {
return {};
}
vector<string> combs;
const vector<string> chars = { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
string builder;
build(builder, 0, digits, chars, combs);
return combs;
}
void Recursion(vector<string>& vec, string str, int counter, int left, int right) {
if (counter < 0)return;
if (left == 0 && right == 0) {
vec.push_back(str);
return;
}
if (left) {
Recursion(vec, str + "(", counter + 1, left - 1, right);
}
if (right) {
Recursion(vec, str + ")", counter - 1, left, right - 1);
}
}
vector<string> generateParenthesis(int n) {
vector<string> vec;
Recursion(vec, "", 0, n, n);
return vec;
}
int countIter = 0;
void permute(vector<int> nums, int i, vector<vector<int>>& perms) {
countIter++;
if (i == nums.size()) {
perms.push_back(nums);
cout << "********" << i << "**********\n";
}
else {
for (int j = i; j < nums.size(); j++) {
swap(nums[i], nums[j]);
cout << "i: " << i << " j:"<<j <<"\n";
permute(nums, i + 1, perms);
cout << "back i: " << i << " back j:" << j << "\n";
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> perms;
permute(nums, 0, perms);
return perms;
}
void check(vector<vector<char>>& board, int i, int j) {
if (board[i][j] == 'O') {
board[i][j] = '1';
if (i > 1) check(board, i - 1, j);
if (j > 1) check(board, i, j - 1);
if (i + 1 < board.size()) check(board, i + 1, j);
if (j + 1 < board[0].size()) check(board, i, j + 1);
}
}
void solve(vector<vector<char>>& board) {
if (board.empty()) return;
int row = board.size(), col = board[0].size();
for (int i = 0; i < row; ++i) {
check(board, i, 0); // first column
check(board, i, col - 1); // last column
}
for (int j = 1; j < col - 1; ++j) {
check(board, 0, j); // first row
check(board, row - 1, j); // last row
}
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j)
if (board[i][j] == 'O') board[i][j] = 'X';
else if (board[i][j] == '1') board[i][j] = 'O';
}
int numUniqueEmails(vector<string>& emails) {
set<string> setUniqueEmails;
for (auto email : emails)
{
int pos1 = email.find('+');
int pos2 = email.find('@');
cout << pos2;
string sNewEmail;
for (int i = 0; i < email.size(); ++i)
{
if (i < pos2 && email[i] == '.')
continue;
if (email[i] == '+')
continue;
if (pos1!= -1 && i > pos1&& i < pos2)
continue;
sNewEmail = sNewEmail + email[i];
}
if (!setUniqueEmails.count(sNewEmail))
{
setUniqueEmails.insert(sNewEmail);
}
}
return setUniqueEmails.size();
}
void rotate(vector<int>& nums, int k) {
k = k % nums.size();
int count = 0;
for (int start = 0; count < nums.size(); start++)
{
int current = start;
int prev = nums[start];
do
{
int next = (current + k) % nums.size();
int temp = nums[next];
nums[next] = prev;
prev = temp;
current = next;
count++;
} while (start != current);
}
}
void sortColors(vector<int>& nums) {
int low = 0, mid = 0, high = nums.size() - 1;
while (mid <= high)
{
if (nums[mid] == 0)
{
swap(nums[low], nums[mid]);
low++; mid++;
}
else if (nums[mid] == 1)
{
mid++;
}
else if (nums[mid] == 2)
{
swap(nums[mid], nums[high]);
high--;
}
}
}
bool wordBreak(string s, vector<string>& wordDict)
{
if (s.empty() || wordDict.empty())
return false;
vector<int> dp(s.size() + 1, 0);
dp[0] = 1;
for (int i = 1; i <= s.size(); i++) {
for (const string& word : wordDict) {
int len = word.size();
if (i - len >= 0 &&
s.substr(i - len, len) == word && dp[i - len] == 1) {
dp[i] = 1;
}
}
}
return dp[s.size()] == 1;
}
void dfs2(int l, int r, string solution, vector<string>& ans) {
if (l == r) {
solution[l] = '0';
ans.push_back(solution);
solution[l] = '1';
ans.push_back(solution);
solution[l] = '8';
ans.push_back(solution);
return;
}
else if (l > r) {
ans.push_back(solution);
return;
}
solution[l] = solution[r] = '1';
dfs2(l + 1, r - 1, solution, ans);
solution[l] = solution[r] = '8';
dfs2(l + 1, r - 1, solution, ans);
solution[l] = '6', solution[r] = '9';
dfs2(l + 1, r - 1, solution, ans);
solution[l] = '9', solution[r] = '6';
dfs2(l + 1, r - 1, solution, ans);
if (l != 0) {
solution[l] = solution[r] = '0';
dfs2(l + 1, r - 1, solution, ans);
}
}
vector<string> findStrobogrammatic(int n) {
vector<string> ans;
if (n <= 0) return ans;
string solution;
for (int i = 0; i < n; i++) solution += "0";
int l = 0, r = n - 1;
dfs2(l, r, solution, ans);
return ans;
}
string reorganizeString(string str) {
int strSize = str.size();
if (strSize < 2)
return str;
vector<int> freq(26, 0);
int freqChar = -1, freqCharCount = -1;
for (int i = 0; i < strSize; i++) {
freq[str[i] - 'a']++;
if (freq[str[i] - 'a'] > freqCharCount) {
freqCharCount = freq[str[i] - 'a'];
freqChar = str[i] - 'a';
}
}
if (freqCharCount > (strSize + 1) / 2)
return "";
int position = 0;
for (int i = -1; i < 26; i++) {
int current = (i == -1) ? freqChar : i;
while (freq[current]-- > 0) {
str[position] = 'a' + current;
position = (position + 2 >= strSize) ? 1 : position + 2;
}
}
return str;
}
string rearrangeString(string str, int k) {
int maxFreq = 0;
int freqs[26];
memset(freqs, 0, sizeof(freqs));
for (char c : str) {
freqs[c - 'a']++;
maxFreq = max(maxFreq, freqs[c - 'a']);
}
vector<string> buckets(maxFreq, "");
for (int i = 0; i < 26; i++) {
if (freqs[i] == maxFreq) {
char c = 'a' + i;
for (int k = 0; k < freqs[i]; k++) {
buckets[k] += c;
}
}
}
for (int i = 0; i < 26; i++) {
if (freqs[i] == maxFreq - 1) {
char c = 'a' + i;
for (int k = 0; k < freqs[i]; k++) {
buckets[k] += c;
}
}
}
int bIdx = 0;
for (int i = 0; i < 26; i++) {
if (freqs[i] < maxFreq - 1) {
char c = 'a' + i;
for (int k = 0; k < freqs[i]; k++) {
buckets[bIdx] += c;
bIdx = (bIdx + 1) % (maxFreq - 1);
}
}
}
string ans = "";
for (int i = 0; i < maxFreq; i++) {
if (i < maxFreq - 1 && buckets[i].length() < k) {
return "";
}
ans += buckets[i];
}
return ans;
}
string countAndSay(int n)
{
if (n == 0) return "";
string res = "1";
while (--n)
{
string cur = "";
for (int i = 0; i < res.size(); i++)
{
int count = 1;
while ((i + 1 < res.size()) && (res[i] == res[i + 1]))
{
count++;
i++;
}
cur += to_string(count) + res[i];
}
res = cur;
}
return res;
}
void moveZeroes(vector<int>& nums) {
int last = 0, cur = 0;
while (cur < nums.size()) {
if (nums[cur] != 0) {
swap(nums[last], nums[cur]);
last++;
}
cur++;
}
}
int findPeakElement(const vector<int>& num)
{
int low = 0;
int high = num.size() - 1;
while (low < high)
{
int mid1 = (low + high) / 2;
int mid2 = mid1 + 1;
if (num[mid1] < num[mid2])
low = mid2;
else
high = mid1;
}
return low;
}
unordered_set<string> words;
void constructSentence(const string& s,
const vector<int>& dp,
int pos,
vector<string>& res,
string temp) {
for (int i = pos + 1; i < dp.size(); ++i) {
if (dp[i]) {
string new_word = s.substr(pos, i - pos);
if (words.count(new_word)) {
if (i + 1 == dp.size()) {
res.push_back(temp + new_word);
return;
}
constructSentence(s, dp, i, res, temp + new_word + " ");
}
}
}
}
vector<string> wordBreakII(string s, vector<string>& wordDict) {
words.clear();
copy(wordDict.begin(), wordDict.end(), std::inserter(words, words.end()));
int n = s.size();
vector<int> dp(n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = i - 1; j >= 0; --j) {
if (dp[j]) {
if (words.count(s.substr(j, i - j))) {
dp[i] = 1;
break;
}
}
}
}
vector<string> res;
if (dp[n]) {
constructSentence(s, dp, 0, res, "");
}
return res;
}
/* bool two_dig(string s, int beg)
{
return (s[beg - 1] == '1' || (s[beg - 1] == '2' && s[beg] <= '6'));
}
int numDecodings(string s)
{
int len = s.length();
if (len == 0 || s[0] == '0')
return 0;
int res = 0;
vector<int > dp(len, 0);
dp[0] = 1;
if (s[1] > '0')
dp[1] = 1;
if (two_dig(s, 1))
dp[1] += 1;
for (int i = 2; i < len; ++i) {
bool f = (s[i] > '0' || two_dig(s, i));
if (!f)
return 0;
if (s[i] > '0')
dp[i] = dp[i - 1];
if (t
}
}*/
int longestConsecutive(vector<int>& num) {
unordered_map<int, int> m;
int r = 0;
for (int i : num)
{
if (m[i]) continue;
int before = m[i - 1];
int after = m[i + 1];
int atIM1 = m[i - m[i - 1]];
int atIP1 = m[i + m[i + 1]];
int val = m[i] = m[i + m[i + 1]] = m[i - m[i - 1]] = m[i + 1] + m[i - 1] + 1;
r = max(r, val);
}
return r;
}
int kEmptySlots(vector<int>& flowers, int k) {
vector<int> days(flowers.size());
for (int i = 0; i < flowers.size(); i++)days[flowers[i] - 1] = i + 1;
int left = 0, right = k + 1, res = INT_MAX;
for (int i = 0; right < days.size(); i++) {
if (days[i] < days[left] || days[i] <= days[right]) {
if (i == right)res = min(res, max(days[left], days[right])); //we get a valid subarray
left = i, right = k + 1 + i;
}
}
return (res == INT_MAX) ? -1 : res;
}
string nextClosestTime(string time)
{
set<char> sorted;
for (auto c : time)
{
if (c == ':') continue;
sorted.insert(c);
}
string res = time;
for (int i = time.size() - 1; i >= 0; i--)
{
if (time[i] == ':') continue;
auto it = sorted.find(time[i]);
if (*it != *sorted.rbegin())
{// not the largest number
it++; // go to the next element
res[i] = *it;
if ((i >= 3 && stoi(res.substr(3, 2)) < 60) || (i < 2 && stoi(res.substr(0, 2)) < 24))
return res;
}
res[i] = *sorted.begin(); // take the smallest number
}
return res;
}
bool similar(vector<int>& a)
{
for (int i = 1; i < a.size(); ++i)
{
if (a[i] != a[i - 1])
{
return false;
}
}
return true;
}
int minDominoRotations(vector<int>& A, vector<int>& B) {
if (A.size() != B.size()) return -1;
map<int, int> mpA;
map<int, int> mpB;
map<int, int> ::iterator it;
for (int i = 0; i < A.size(); ++i)
{
mpA[A[i]]++;
mpB[B[i]]++;
}
//get the highest frequency element from both the map
pair<int, int> maxA;;
pair<int, int> maxB;;
for (it = mpA.begin(); it != mpA.end(); ++it)
{
if (it->second > maxA.second)
{
maxA.second = it->second;
maxA.first = it->first;
}
}
for (it = mpB.begin(); it != mpB.end(); ++it)
{
if (it->second > maxB.second)
{
maxB.second = it->second;
maxB.first = it->first;
}
}
int i = 0; bool bIsA = false;
int swap = 0, item = 0;
if (maxA.second > maxB.second)
{
item = maxA.first;
bIsA = true;
}
else
{
item = maxB.first;
}
while (i < A.size())
{
if (bIsA)
{
//item to be replaced in A
if (A[i] != B[i] && A[i] != item)
{
int val = B[i];
B[i] = A[i];
A[i] = B[i];
swap++;
//check if(all the elements are similar)
if (similar(A))
return swap;
}
}
else
{
//item to be replaced in B
}
i++;
}
return -1;
}
void duplicateZeros(vector<int>& A) {
int n = A.size(), j = n + count(A.begin(), A.end(), 0);
for (int i = n - 1; i >= 0; --i)
{
if (--j < n)
A[j] = A[i];
if (A[i] == 0 && --j < n)
A[j] = 0;
}
}
void bfs(vector<vector<int>>& graph, vector<int>& color, int i, bool bIs_bipartite)
{
queue<int> q;
color[i] = 1;
q.push(i);
while (!q.empty())
{
int u = q.front(); q.pop();
for (int v : graph[u])
{
if (color[v] == 2) //not visited
{
color[v] = 1 - color[u];
q.push(v);
}
else if (color[u] == color[v])
{
bIs_bipartite = false;
return;
}
}
}
}
bool isBipartite(vector<vector<int>>& graph)
{
vector<int> color(graph.size(), 2);
bool bIs_bipartite = true;
for (int i = 0; i < graph.size(); ++i)
{
if (color[i] == 2)
{
bfs(graph, color, i, bIs_bipartite);
}
}
return bIs_bipartite;
}
int findDuplicate(vector<int>& nums) {
int result = 0;
for (int i = 1; i < nums.size(); ++i)
{
if (nums[i] == nums[i - 1])
{
return nums[i];
}
if (nums[i] < nums[i - 1])
{
int j = i;
while (j - 1 >= 0 && nums[j - 1] > nums[j])
{
swap(nums[j], nums[j - 1]);
j--;
if (j - 1 >= 0 && nums[j] == nums[j - 1])
return true;
}
}
}
return result;
}
bool isPossible(vector<int>& nums) {
unordered_map<int, int> cnt, tails;
for (int& i : nums) cnt[i]++;
for (int& i : nums) {
if (!cnt[i]) continue;
cnt[i]--;
if (tails[i - 1] > 0) {
tails[i - 1]--;
tails[i]++;
}
else if (cnt[i + 1] && cnt[i + 2]) {
cnt[i + 1]--;
cnt[i + 2]--;
tails[i + 2]++;
}
else return false;
}
return true;
}
string addBinary(string a, string b)
{
string result = ""; // Initialize result
int s = 0; // Initialize digit sum
// Traverse both strings starting from last
// characters
int i = a.size() - 1, j = b.size() - 1;
while (i >= 0 || j >= 0 || s == 1)
{
// Comput sum of last digits and carry
s += ((i >= 0) ? a[i] - '0' : 0);
s += ((j >= 0) ? b[j] - '0' : 0);
// If current digit sum is 1 or 3, add 1 to result
result = char(s % 2 + '0') + result;
// Compute carry
s /= 2;
// Move to next digits
i--; j--;
}
return result;
}
bool isSubSwequence(string source, string target)
{
int sL = source.length() - 1;
int tL = target.length() - 1;
int i = 0, j = 0;
while (i <= sL && j <= tL)
{
if (source[i] == target[j])
{
i++;
j++;
}
i++;
}
return j == tL - 1;
}
int numMatchingSubseq(string S, vector<string>& words) {
int count = 0;
for (auto& word : words)
{
if (isSubSwequence(S, word))
{
count++;
}
}
return count;
}
vector<int> dest;
vector<vector<int>> pos;
vector<vector<int>> vec;
int minKnightMoves(int x, int y)
{
if (x == 0 && y == 0)
return 0;
queue<pair<int, int>> q;
queue<int> level;
set<pair<int, int>> v;
vector<vector<int>> dir = { {1,2},{2,1},{2,-1},{1,-2},{-1,2},{-2,1},{-1,-2},{-2,-1} };
q.push(make_pair(0, 0));
v.insert(make_pair(0, 0));
level.push(0);
pair<int, int> temp;
while (!q.empty())
{
int l = level.front();
level.pop();
pair<int, int> cur = q.front();
q.pop();
if (cur.first == abs(x) && cur.second == abs(y))
return l;
for (int i = 0; i < dir.size(); i++)
{
temp.first = abs(cur.first + dir[i][0]);
temp.second = abs(cur.second + dir[i][1]);
if (v.find(temp) == v.end())
{
v.insert(temp);
q.push(temp);
level.push(l + 1);
}
}
}
return -1;
}
int n, m;
vector<vector<int>>& flip(vector<vector<int>>& mat, int y, int x) {
mat[y][x] ^= 1;
if (y - 1 >= 0) mat[y - 1][x] ^= 1;
if (y + 1 < n) mat[y + 1][x] ^= 1;
if (x - 1 >= 0) mat[y][x - 1] ^= 1;
if (x + 1 < m) mat[y][x + 1] ^= 1;
return mat;
}
bool isZeroMat(vector<vector<int>>& mat) {
for (auto i = 0; i < n; ++i)
for (auto j = 0; j < m; ++j)
if (mat[i][j])
return false;
return true;
}
int FlipOrNotFlip(vector<vector<int>> mat, int y, int x) {
if (x == m) y++, x = 0;
if (y == n) return isZeroMat(mat) ? 0 : 10000;
auto ret1 = FlipOrNotFlip(mat, y, x + 1);
auto ret2 = FlipOrNotFlip(flip(mat, y, x), y, x + 1) + 1;
return min(ret1, ret2);
}
int minFlips(vector<vector<int>>& mat) {
n = mat.size(), m = mat[0].size();
auto ret = FlipOrNotFlip(mat, 0, 0);
return (ret >= 10000 ? -1 : ret);
}