Skip to content

Commit e8359dc

Browse files
committed
2 parents 7c07bcd + 4daa127 commit e8359dc

File tree

8 files changed

+242
-0
lines changed

8 files changed

+242
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Node {
5+
public:
6+
int val;
7+
Node *next;
8+
Node(int v) {
9+
val = v;
10+
next = NULL;
11+
}
12+
};
13+
14+
void insertAtStart(Node* &head, int v) {
15+
Node *n = new Node(v);
16+
if (head == NULL) {
17+
head = n;
18+
return;
19+
}
20+
n->next = head;
21+
head = n;
22+
}
23+
24+
Node* midOfLL(Node* head) {
25+
if (head == NULL)
26+
return head;
27+
if (head->next == NULL)
28+
return head;
29+
30+
Node* s = head;
31+
Node* f = head->next;
32+
33+
while (f != NULL && f->next != NULL) {
34+
s = s->next;
35+
f = f->next;
36+
f = f->next;
37+
}
38+
return s;
39+
}
40+
41+
int main() {
42+
Node* head = NULL;
43+
insertAtStart(head, 6);
44+
insertAtStart(head, 5);
45+
insertAtStart(head, 4);
46+
insertAtStart(head, 3);
47+
insertAtStart(head, 2);
48+
insertAtStart(head, 1);
49+
50+
Node* mid = midOfLL(head);
51+
cout << mid->val << endl;
52+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Middle of the Linked List
2+
3+
Given the head of a singly linked list, return the middle node of the linked list.
4+
If there are even nodes, then there would be two middle nodes, we need to print the second middle element.
5+
6+
# Examples:
7+
8+
## Example 1:
9+
If the given linked list is 1->2->3->4->5 then the output should be 3.
10+
11+
## Example 2:
12+
If given linked list is 1->2->3->4->5->6 then the output should be 4.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Implement KMP algorithm for pattern search
2+
3+
##
4+
5+
#### Time Complexity O(N) which best possible time we can get in any pattern serching algorithm
6+
7+
This Function return indext of first element of match pattern elements as output.
8+
9+
10+
11+
12+
### Example 1:
13+
```
14+
Input: RDBMSSURSURAJJPG
15+
Pattern to search : SURAJ
16+
Output: 9
17+
18+
```
19+
### Example 2:
20+
```
21+
Input: SEROJSHASHESHERSHAH
22+
Pattern to search : SHERSHAH
23+
Output: 12
24+
25+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//KMP ALGOTITHM IN CPP
2+
3+
#include <bits/stdc++.h>
4+
#include <iostream>
5+
6+
void computeLPSArray(char* pat, int M, int* lps);
7+
8+
9+
void KMPSearch(char* pat, char* txt)
10+
{
11+
int M = strlen(pat);
12+
int N = strlen(txt);
13+
int lps[M];
14+
15+
computeLPSArray(pat, M, lps);
16+
17+
int i = 0;
18+
int j = 0;
19+
while (i < N) {
20+
if (pat[j] == txt[i]) {
21+
j++;
22+
i++;
23+
}
24+
25+
if (j == M) {
26+
printf("Found pattern at index %d ", i - j);
27+
j = lps[j - 1];
28+
}
29+
30+
31+
else if (i < N && pat[j] != txt[i]) {
32+
33+
if (j != 0)
34+
j = lps[j - 1];
35+
else
36+
i = i + 1;
37+
}
38+
}
39+
}
40+
41+
void computeLPSArray(char* pat, int M, int* lps)
42+
{
43+
int len = 0;
44+
45+
lps[0] = 0;
46+
int i = 1;
47+
while (i < M) {
48+
if (pat[i] == pat[len]) {
49+
len++;
50+
lps[i] = len;
51+
i++;
52+
}
53+
else
54+
{
55+
56+
if (len != 0) {
57+
len = lps[len - 1];
58+
}
59+
else
60+
{
61+
lps[i] = 0;
62+
i++;
63+
}
64+
}
65+
}
66+
}
67+
68+
69+
int main()
70+
{
71+
char txt[] = "ABABDABACDABABCABAB";
72+
char pat[] = "ABABCABAB";
73+
KMPSearch(pat, txt);
74+
return 0;
75+
}

Social/banner.png

-15.7 KB
Loading

Stack/Valid Parenthesis/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Problem Statement
2+
- You have been given a string s which consists of only characters '[', ']', '(', ')', '{', and '}'. Your task is to check whether the string is valid or not.
3+
1. Open brackets has to be closed with closing brackets properly.
4+
2. Open and closed brackets has to be in proper order.
5+
6+
# Input Format
7+
8+
You will be given a string 's'.
9+
10+
Constraints
11+
```
12+
1 <= s.length <= 104
13+
s consists of parentheses only i.e. '()[]{}'.
14+
```
15+
# Output Format
16+
17+
The task is to complete the function isValid() which takes a string as a parameter. You will have to return true after checking if the string is valid otherwise false.
18+
19+
Sample Input 1
20+
```
21+
"()[]{}"
22+
```
23+
Sample Output 1
24+
```
25+
true
26+
```
27+
28+
Sample Input 2
29+
```
30+
"([)]"
31+
```
32+
Sample Output 2
33+
```
34+
false
35+
```
36+
37+
Explanation
38+
```
39+
40+
All the brackets exists in a pair with proper ordering.
41+
s = "(((}", s = "(]", and s="({[]}]" are invalid.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution
5+
{
6+
public:
7+
bool isParenthesisValid(string s)
8+
{
9+
stack<int> stk;
10+
11+
for (auto ch : s)
12+
{
13+
if (ch == '(' or ch == '[' or ch == '{')
14+
stk.push(ch);
15+
16+
else {
17+
if(stk.empty() or (stk.top() == '(' and ch != ')') or (stk.top() == '[' and ch != ']') or (stk.top() == '{' and ch != '}')) return false;
18+
stk.pop();
19+
}
20+
21+
}
22+
23+
return stk.empty();
24+
}
25+
};
26+
27+
int main()
28+
{
29+
string s;
30+
cin >> s;
31+
Solution *obj = new Solution;
32+
bool valid = obj->isParenthesisValid(s);
33+
if (valid)
34+
cout << "Valid" << '\n';
35+
else
36+
cout << "Invalid" << '\n';
37+
}

website/src/error.png

-1.2 KB
Loading

0 commit comments

Comments
 (0)