Skip to content

Commit 68f33b5

Browse files
authored
Sri hari: Batch-8/Added Articles/Neetcode-ALL (#4318)
* Batch-8/Neetcode-ALL/Added-articles * Batch-8/Neetcode-ALL/Added-articles * Batch-8/Neetcode-ALL/Added-articles * Batch-8/Neetcode-ALL/Added-articles * Batch-8/Neetcode-ALL/Added-articles * Batch-8/Neetcode-ALL/Added-articles
1 parent d8f0d05 commit 68f33b5

13 files changed

+4406
-3
lines changed

articles/convert-bst-to-greater-tree.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,45 @@ class Solution {
155155
}
156156
```
157157

158+
```csharp
159+
/**
160+
* Definition for a binary tree node.
161+
* public class TreeNode {
162+
* public int val;
163+
* public TreeNode left;
164+
* public TreeNode right;
165+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
166+
* this.val = val;
167+
* this.left = left;
168+
* this.right = right;
169+
* }
170+
* }
171+
*/
172+
public class Solution {
173+
public TreeNode ConvertBST(TreeNode root) {
174+
int GetSum(TreeNode node) {
175+
if (node == null) return 0;
176+
return node.val + GetSum(node.left) + GetSum(node.right);
177+
}
178+
179+
int totalSum = GetSum(root);
180+
181+
void Dfs(TreeNode node) {
182+
if (node == null) return;
183+
184+
Dfs(node.left);
185+
int tmp = node.val;
186+
node.val = totalSum;
187+
totalSum -= tmp;
188+
Dfs(node.right);
189+
}
190+
191+
Dfs(root);
192+
return root;
193+
}
194+
}
195+
```
196+
158197
::tabs-end
159198

160199
### Time & Space Complexity
@@ -299,6 +338,40 @@ class Solution {
299338
}
300339
```
301340

341+
```csharp
342+
/**
343+
* Definition for a binary tree node.
344+
* public class TreeNode {
345+
* public int val;
346+
* public TreeNode left;
347+
* public TreeNode right;
348+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
349+
* this.val = val;
350+
* this.left = left;
351+
* this.right = right;
352+
* }
353+
* }
354+
*/
355+
public class Solution {
356+
public TreeNode ConvertBST(TreeNode root) {
357+
int curSum = 0;
358+
359+
void Dfs(TreeNode node) {
360+
if (node == null) return;
361+
362+
Dfs(node.right);
363+
int tmp = node.val;
364+
node.val += curSum;
365+
curSum += tmp;
366+
Dfs(node.left);
367+
}
368+
369+
Dfs(root);
370+
return root;
371+
}
372+
}
373+
```
374+
302375
::tabs-end
303376

304377
### Time & Space Complexity
@@ -448,6 +521,43 @@ class Solution {
448521
}
449522
```
450523

524+
```csharp
525+
/**
526+
* Definition for a binary tree node.
527+
* public class TreeNode {
528+
* public int val;
529+
* public TreeNode left;
530+
* public TreeNode right;
531+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
532+
* this.val = val;
533+
* this.left = left;
534+
* this.right = right;
535+
* }
536+
* }
537+
*/
538+
public class Solution {
539+
public TreeNode ConvertBST(TreeNode root) {
540+
int curSum = 0;
541+
Stack<TreeNode> stack = new Stack<TreeNode>();
542+
TreeNode node = root;
543+
544+
while (stack.Count > 0 || node != null) {
545+
while (node != null) {
546+
stack.Push(node);
547+
node = node.right;
548+
}
549+
550+
node = stack.Pop();
551+
curSum += node.val;
552+
node.val = curSum;
553+
node = node.left;
554+
}
555+
556+
return root;
557+
}
558+
}
559+
```
560+
451561
::tabs-end
452562

453563
### Time & Space Complexity
@@ -635,6 +745,53 @@ class Solution {
635745
}
636746
```
637747

748+
```csharp
749+
/**
750+
* Definition for a binary tree node.
751+
* public class TreeNode {
752+
* public int val;
753+
* public TreeNode left;
754+
* public TreeNode right;
755+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
756+
* this.val = val;
757+
* this.left = left;
758+
* this.right = right;
759+
* }
760+
* }
761+
*/
762+
public class Solution {
763+
public TreeNode ConvertBST(TreeNode root) {
764+
int curSum = 0;
765+
TreeNode cur = root;
766+
767+
while (cur != null) {
768+
if (cur.right != null) {
769+
TreeNode prev = cur.right;
770+
while (prev.left != null && prev.left != cur) {
771+
prev = prev.left;
772+
}
773+
774+
if (prev.left == null) {
775+
prev.left = cur;
776+
cur = cur.right;
777+
} else {
778+
prev.left = null;
779+
curSum += cur.val;
780+
cur.val = curSum;
781+
cur = cur.left;
782+
}
783+
} else {
784+
curSum += cur.val;
785+
cur.val = curSum;
786+
cur = cur.left;
787+
}
788+
}
789+
790+
return root;
791+
}
792+
}
793+
```
794+
638795
::tabs-end
639796

640797
### Time & Space Complexity

articles/find-the-index-of-the-first-occurrence-in-a-string.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ class Solution {
8080
}
8181
```
8282

83+
```csharp
84+
public class Solution {
85+
public int StrStr(string haystack, string needle) {
86+
int n = haystack.Length, m = needle.Length;
87+
for (int i = 0; i <= n - m; i++) {
88+
int j = 0;
89+
while (j < m) {
90+
if (haystack[i + j] != needle[j]) {
91+
break;
92+
}
93+
j++;
94+
}
95+
if (j == m) {
96+
return i;
97+
}
98+
}
99+
return -1;
100+
}
101+
}
102+
```
103+
83104
::tabs-end
84105

85106
### Time & Space Complexity
@@ -274,6 +295,52 @@ class Solution {
274295
}
275296
```
276297

298+
```csharp
299+
public class Solution {
300+
public int StrStr(string haystack, string needle) {
301+
if (needle == "") return 0;
302+
303+
int[] lps = new int[needle.Length];
304+
int prevLPS = 0, i = 1;
305+
306+
while (i < needle.Length) {
307+
if (needle[i] == needle[prevLPS]) {
308+
lps[i] = prevLPS + 1;
309+
prevLPS++;
310+
i++;
311+
} else if (prevLPS == 0) {
312+
lps[i] = 0;
313+
i++;
314+
} else {
315+
prevLPS = lps[prevLPS - 1];
316+
}
317+
}
318+
319+
i = 0;
320+
int j = 0;
321+
322+
while (i < haystack.Length) {
323+
if (haystack[i] == needle[j]) {
324+
i++;
325+
j++;
326+
} else {
327+
if (j == 0) {
328+
i++;
329+
} else {
330+
j = lps[j - 1];
331+
}
332+
}
333+
334+
if (j == needle.Length) {
335+
return i - needle.Length;
336+
}
337+
}
338+
339+
return -1;
340+
}
341+
}
342+
```
343+
277344
::tabs-end
278345

279346
### Time & Space Complexity
@@ -423,6 +490,41 @@ class Solution {
423490
}
424491
```
425492

493+
```csharp
494+
public class Solution {
495+
public int StrStr(string haystack, string needle) {
496+
if (string.IsNullOrEmpty(needle)) return 0;
497+
498+
string s = needle + "$" + haystack;
499+
int n = s.Length;
500+
int[] z = new int[n];
501+
int l = 0, r = 0;
502+
503+
for (int i = 1; i < n; i++) {
504+
if (i <= r) {
505+
z[i] = Math.Min(r - i + 1, z[i - l]);
506+
}
507+
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
508+
z[i]++;
509+
}
510+
if (i + z[i] - 1 > r) {
511+
l = i;
512+
r = i + z[i] - 1;
513+
}
514+
}
515+
516+
int m = needle.Length;
517+
for (int i = m + 1; i < n; i++) {
518+
if (z[i] == m) {
519+
return i - m - 1;
520+
}
521+
}
522+
523+
return -1;
524+
}
525+
}
526+
```
527+
426528
::tabs-end
427529

428530
### Time & Space Complexity
@@ -623,6 +725,52 @@ class Solution {
623725
}
624726
```
625727

728+
```csharp
729+
public class Solution {
730+
public int StrStr(string haystack, string needle) {
731+
if (string.IsNullOrEmpty(needle)) return 0;
732+
733+
int base1 = 31, mod1 = 768258391;
734+
int base2 = 37, mod2 = 685683731;
735+
736+
int n = haystack.Length, m = needle.Length;
737+
if (m > n) return -1;
738+
739+
long power1 = 1, power2 = 1;
740+
for (int i = 0; i < m; i++) {
741+
power1 = (power1 * base1) % mod1;
742+
power2 = (power2 * base2) % mod2;
743+
}
744+
745+
long needleHash1 = 0, needleHash2 = 0;
746+
long haystackHash1 = 0, haystackHash2 = 0;
747+
748+
for (int i = 0; i < m; i++) {
749+
needleHash1 = (needleHash1 * base1 + needle[i]) % mod1;
750+
needleHash2 = (needleHash2 * base2 + needle[i]) % mod2;
751+
haystackHash1 = (haystackHash1 * base1 + haystack[i]) % mod1;
752+
haystackHash2 = (haystackHash2 * base2 + haystack[i]) % mod2;
753+
}
754+
755+
for (int i = 0; i <= n - m; i++) {
756+
if (haystackHash1 == needleHash1 && haystackHash2 == needleHash2) {
757+
return i;
758+
}
759+
760+
if (i + m < n) {
761+
haystackHash1 = (haystackHash1 * base1 - haystack[i] * power1 + haystack[i + m]) % mod1;
762+
haystackHash2 = (haystackHash2 * base2 - haystack[i] * power2 + haystack[i + m]) % mod2;
763+
764+
if (haystackHash1 < 0) haystackHash1 += mod1;
765+
if (haystackHash2 < 0) haystackHash2 += mod2;
766+
}
767+
}
768+
769+
return -1;
770+
}
771+
}
772+
```
773+
626774
::tabs-end
627775

628776
### Time & Space Complexity

0 commit comments

Comments
 (0)