Skip to content

Commit 790c799

Browse files
committed
Add day 12
1 parent abf05b0 commit 790c799

File tree

11 files changed

+163
-3
lines changed

11 files changed

+163
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
2828
9. [Day 9 -- Smallest Substring Problem](./day9/) -- [http://codetoexpress.tech/dc/day9/](http://codetoexpress.tech/dc/day9/)
2929
10. [Day 10 -- String Permutation Problem](./day10/) -- [http://codetoexpress.tech/dc/day10/](http://codetoexpress.tech/dc/day10/)
3030
11. [Day 11 -- Longest Common Substring](./day11/) -- [http://codetoexpress.tech/dc/day11/](http://codetoexpress.tech/dc/day11/)
31+
11. [Day 12 -- Substring Search Algorithms](./day12/) -- [http://codetoexpress.tech/dc/day12/](http://codetoexpress.tech/dc/day12/)
3132

3233
## Timeline
3334

3435
<p align="center">
3536
<img src="./timeline.png" alt="timeline">
36-
</p>
37-
37+
</p>

day11/ques.png

4.58 KB
Loading

day12/JavaScript/KMP.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// To Be Added

day12/JavaScript/ZAlgorithm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// To Be Added

day12/JavaScript/boyer_moore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// To Be Added

day12/JavaScript/rabin_karp.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// To Be Added

day12/JavaScript/sol.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 04/01/2018
4+
* In this case, the worst case Time Complexity will be O(m.n),
5+
* m and n are the lengths of string and pattern respectively.
6+
*/
7+
8+
function substringSearch (str, pattern) {
9+
let strLen = str.length,
10+
patLen = pattern.length,
11+
flag = 0;
12+
13+
for (let i=0; i<(strLen-patLen+1); i++) {
14+
if (str[i] === pattern[0]) {
15+
flag = 1;
16+
for (let j=1; j<patLen; j++) {
17+
if (str[i+j] !== pattern[j]) {
18+
flag = 0;
19+
break;
20+
}
21+
}
22+
if (flag === 1) {
23+
console.log (i);
24+
return i;
25+
}
26+
}
27+
}
28+
29+
console.log (-1);
30+
return -1;
31+
}
32+
33+
substringSearch ("helloworld", "world");
34+
substringSearch ("abcrxyzgf", "xyz");

day12/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
![cover](./cover.png)
2+
3+
# day 12 - Substring Search Problem
4+
5+
**Question** -- Given a text, and a pattern, find whether the given pattern exists in the text or not.
6+
7+
**Implement various algorithms**
8+
9+
```
10+
a) Brute Force Search
11+
b) Knuth-Morris-Pratt Algorithm
12+
c) Z Algorithm
13+
d) Rabin Karp Algorithm
14+
e) Boyer Moore Algorithm
15+
```
16+
17+
### Example
18+
19+
```
20+
input: text: helloworld, substring: hello
21+
output: 0 (index of 'h', where the substring starts)
22+
23+
input: text: helloworld, substring: hop
24+
output: -1
25+
26+
input: text: abcrxyzgf, substring: xyz
27+
output: 4
28+
```
29+
30+
![ques](./ques.png)
31+
32+
## A) Brute Force Search
33+
34+
### JavaScript Implementation
35+
36+
#### [Solution](./JavaScript/sol.js)
37+
38+
Substring search can be done using brute force.
39+
In this case, the worst case Time Complexity will be O(m.n), m and n are the lengths of string and pattern respectively.
40+
41+
```js
42+
/**
43+
* @author MadhavBahlMD
44+
* @date 04/01/2018
45+
* In this case, the worst case Time Complexity will be O(m.n),
46+
* m and n are the lengths of string and pattern respectively.
47+
*/
48+
49+
function substringSearch (str, pattern) {
50+
let strLen = str.length,
51+
patLen = pattern.length,
52+
flag = 0;
53+
54+
for (let i=0; i<(strLen-patLen+1); i++) {
55+
if (str[i] === pattern[0]) {
56+
flag = 1;
57+
for (let j=1; j<patLen; j++) {
58+
if (str[i+j] !== pattern[j]) {
59+
flag = 0;
60+
break;
61+
}
62+
}
63+
if (flag === 1) {
64+
console.log (i);
65+
return i;
66+
}
67+
}
68+
}
69+
70+
console.log (-1);
71+
return -1;
72+
}
73+
74+
substringSearch ("helloworld", "world");
75+
substringSearch ("abcrxyzgf", "xyz");
76+
```
77+
78+
## B) Knuth-Morris-Pratt Algorithm
79+
80+
### JavaScript Implementation
81+
82+
#### [Solution](./JavaScript/KMP.js)
83+
84+
```js
85+
To Be Added
86+
```
87+
88+
## C) Z Algorithm
89+
90+
### JavaScript Implementation
91+
92+
#### [Solution](./JavaScript/ZAlgorithm.js)
93+
94+
```js
95+
To Be Added
96+
```
97+
98+
## D) Rabin Karp Algorithm
99+
100+
### JavaScript Implementation
101+
102+
#### [Solution](./JavaScript/rabin_karp.js)
103+
104+
```js
105+
To Be Added
106+
```
107+
108+
## E) Boyer Moore Algorithm
109+
110+
### JavaScript Implementation
111+
112+
#### [Solution](./JavaScript/boyer_moore.js)
113+
114+
```js
115+
To Be Added
116+
```
117+
118+
### Have Another solution?
119+
120+
The beauty of programming lies in the fact that there is never a single solution to any problem.
121+
122+
In case you have an alternative way to solve this problem, do contribute to this repository (https://github.com/CodeToExpress/dailycodebase)

day12/cover.png

164 KB
Loading

day12/ques.png

615 KB
Loading

day8/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,4 @@ main
615615
616616
The beauty of programming lies in the fact that there is never a single solution to any problem.
617617
618-
In case you have an alternative way to solve this problem, do contribute to this repository (https://github.com/CodeToExpress/dailycodebase) :)
618+
In case you have an alternative way to solve this problem, do contribute to this repository (https://github.com/CodeToExpress/dailycodebase)

0 commit comments

Comments
 (0)