Skip to content

Commit 50984ea

Browse files
committed
Add day 11
1 parent 996eb9f commit 50984ea

File tree

6 files changed

+206
-1
lines changed

6 files changed

+206
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
2727
8. [Day 8 -- Minimum Edit Distance](./day8/) -- [http://codetoexpress.tech/dc/day8/](http://codetoexpress.tech/dc/day8/)
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/)
30+
11. [Day 11 -- Longest Common Substring](./day11/) -- [http://codetoexpress.tech/dc/day11/](http://codetoexpress.tech/dc/day11/)
3031

3132
## Timeline
3233

day10/README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,71 @@ output:
2222

2323
## JavaScript Implementation
2424

25-
### [Solution](./sol.js)
25+
### [Solution using recursion](./JavaScript/stringPermute.js)
26+
27+
```js
28+
function stringPermutations (str) {
29+
let permutations = [];
30+
31+
if (str.length <= 1) {
32+
permutations.push (str);
33+
return permutations;
34+
} else {
35+
for (let i=0; i<str.length; i++) {
36+
let start = str[i],
37+
remainingString= str.slice(0, i) + str.slice(i+1),
38+
permutationsforRemainingString = stringPermutations(remainingString);
39+
40+
for (let j=0; j<permutationsforRemainingString.length; j++) {
41+
permutations.push (start + permutationsforRemainingString[j]);
42+
}
43+
}
44+
}
45+
46+
return permutations;
47+
}
48+
49+
console.log (stringPermutations('123'));
50+
```
51+
52+
### [Solution using backtracking](./JavaScript/stringPermute2.js)
53+
54+
```js
55+
/**
56+
* METHOD -- Backtracking
57+
* Algorithm taken from GeeksForGeeks (https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/)
58+
* Implemented in JS by @MadhavBahlMD
59+
* @date 02/01/2019
60+
*/
61+
62+
function swap (str, pos1, pos2) {
63+
// console.log (`pos1 = ${pos1}, pos2 = ${pos2} old`, str);
64+
str = str.split('');
65+
let temp = str[pos1];
66+
str[pos1] = str[pos2];
67+
str[pos2] = temp;
68+
str = str.join('');
69+
// console.log ('new str', str);
70+
return str;
71+
}
72+
73+
function stringPermutations (str, start, end) {
74+
if (start === end) {
75+
console.log (str);
76+
} else {
77+
for (let i=start; i<end; i++) {
78+
str = swap (str, start, i);
79+
stringPermutations (str, start+1, end);
80+
str = swap (str, i, start);
81+
}
82+
}
83+
}
84+
85+
let inputStr = '123';
86+
stringPermutations (inputStr, 0, inputStr.length);
87+
```
88+
89+
### [Solution by @Karthikeyan](./JavaScript/sol.js)
2690

2791
```js
2892
/*
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 03/01/2018
4+
* Referance: https://en.wikipedia.org/wiki/Longest_common_substring_problem
5+
*/
6+
7+
function longestSubstring (str1, str2) {
8+
// initialize the longest substring matrix with all zeroes
9+
let strMat = [],
10+
len1 = str1.length,
11+
len2 = str2.length;
12+
13+
for (let i=0; i<=len2; i++) {
14+
let row = [];
15+
for (let j=0; j<=len1; j++) {
16+
row.push(0);
17+
}
18+
19+
strMat.push(row);
20+
}
21+
22+
// Fill the longest substring matrix and find the maximum element simultaneously
23+
let maxi = 0, maxj = 0, max = strMat[0][0];
24+
for (let i=1; i<=len2; i++) {
25+
for (let j=1; j<=len1; j++) {
26+
if (str2[i-1] === str1[j-1]) {
27+
strMat[i][j] = strMat[i-1][j-1] + 1;
28+
if (strMat[i][j] > max) {
29+
max = strMat[i][j];
30+
maxi = i;
31+
maxj = j;
32+
}
33+
}
34+
}
35+
}
36+
37+
// Find the longest substring
38+
let maxSubStr = '';
39+
for (i=maxi, j=maxj; i>=0; i--, j--) {
40+
if (!(i<=0 || j<=0) && strMat[i][j] !== 0) {
41+
maxSubStr += str2[i-1];
42+
} else {
43+
break;
44+
}
45+
}
46+
47+
// Return the reverse of maxSubStr
48+
return maxSubStr.split('').reverse().join('');
49+
}
50+
51+
console.log(longestSubstring ("abcdefg", "xyabcz"));
52+
console.log(longestSubstring ("XYXYXYZ", "XYZYX"));

day11/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
![cover](./cover.png)
2+
3+
# Day 11 - Longest Common Substring
4+
5+
**Question** -- Given two strings, write a program to find the longest string that is a substring of both the given strings.
6+
7+
### Example
8+
9+
```
10+
input: str1 = "abcdefg", str2 = "xyabcz"
11+
output: "abc"
12+
13+
input: str1 = "XYXYXYZ", str2 = "XYZYX"
14+
output: "XYZ"
15+
```
16+
17+
### Illustration from [Wikipedia](https://en.wikipedia.org/wiki/Longest_common_substring_problem)
18+
19+
The longest common substring of the strings "ABABC", "BABCA" and "ABCBA" is string "ABC" of length 3. Other common substrings are "A", "AB", "B", "BA", "BC" and "C".
20+
21+
```
22+
ABABC
23+
|||
24+
BABCA
25+
|||
26+
ABCBA
27+
```
28+
29+
## JavaScript Implementation
30+
31+
### [Solution using dynamic programming](./JavaScript/longest_substring_dynamic.js)
32+
33+
**Reference** -- [Wikipedia](https://en.wikipedia.org/wiki/Longest_common_substring_problem#Dynamic_programming)
34+
35+
```js
36+
/**
37+
* @author MadhavBahlMD
38+
* @date 03/01/2018
39+
* Referance: https://en.wikipedia.org/wiki/Longest_common_substring_problem
40+
*/
41+
42+
function longestSubstring (str1, str2) {
43+
// initialize the longest substring matrix with all zeroes
44+
let strMat = [],
45+
len1 = str1.length,
46+
len2 = str2.length;
47+
48+
for (let i=0; i<=len2; i++) {
49+
let row = [];
50+
for (let j=0; j<=len1; j++) {
51+
row.push(0);
52+
}
53+
54+
strMat.push(row);
55+
}
56+
57+
// Fill the longest substring matrix and find the maximum element simultaneously
58+
let maxi = 0, maxj = 0, max = strMat[0][0];
59+
for (let i=1; i<=len2; i++) {
60+
for (let j=1; j<=len1; j++) {
61+
if (str2[i-1] === str1[j-1]) {
62+
strMat[i][j] = strMat[i-1][j-1] + 1;
63+
if (strMat[i][j] > max) {
64+
max = strMat[i][j];
65+
maxi = i;
66+
maxj = j;
67+
}
68+
}
69+
}
70+
}
71+
72+
// Find the longest substring
73+
let maxSubStr = '';
74+
for (i=maxi, j=maxj; i>=0; i--, j--) {
75+
if (!(i<=0 || j<=0) && strMat[i][j] !== 0) {
76+
maxSubStr += str2[i-1];
77+
} else {
78+
break;
79+
}
80+
}
81+
82+
// Return the reverse of maxSubStr
83+
return maxSubStr.split('').reverse().join('');
84+
}
85+
86+
console.log(longestSubstring ("abcdefg", "xyabcz"));
87+
console.log(longestSubstring ("XYXYXYZ", "XYZYX"));
88+
```

day11/cover.png

141 KB
Loading

day11/ques.png

889 KB
Loading

0 commit comments

Comments
 (0)