Skip to content

Commit

Permalink
Add day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 3, 2019
1 parent 996eb9f commit 50984ea
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -27,6 +27,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
8. [Day 8 -- Minimum Edit Distance](./day8/) -- [http://codetoexpress.tech/dc/day8/](http://codetoexpress.tech/dc/day8/)
9. [Day 9 -- Smallest Substring Problem](./day9/) -- [http://codetoexpress.tech/dc/day9/](http://codetoexpress.tech/dc/day9/)
10. [Day 10 -- String Permutation Problem](./day10/) -- [http://codetoexpress.tech/dc/day10/](http://codetoexpress.tech/dc/day10/)
11. [Day 11 -- Longest Common Substring](./day11/) -- [http://codetoexpress.tech/dc/day11/](http://codetoexpress.tech/dc/day11/)

## Timeline

Expand Down
66 changes: 65 additions & 1 deletion day10/README.md
Expand Up @@ -22,7 +22,71 @@ output:

## JavaScript Implementation

### [Solution](./sol.js)
### [Solution using recursion](./JavaScript/stringPermute.js)

```js
function stringPermutations (str) {
let permutations = [];

if (str.length <= 1) {
permutations.push (str);
return permutations;
} else {
for (let i=0; i<str.length; i++) {
let start = str[i],
remainingString= str.slice(0, i) + str.slice(i+1),
permutationsforRemainingString = stringPermutations(remainingString);

for (let j=0; j<permutationsforRemainingString.length; j++) {
permutations.push (start + permutationsforRemainingString[j]);
}
}
}

return permutations;
}

console.log (stringPermutations('123'));
```

### [Solution using backtracking](./JavaScript/stringPermute2.js)

```js
/**
* METHOD -- Backtracking
* Algorithm taken from GeeksForGeeks (https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/)
* Implemented in JS by @MadhavBahlMD
* @date 02/01/2019
*/

function swap (str, pos1, pos2) {
// console.log (`pos1 = ${pos1}, pos2 = ${pos2} old`, str);
str = str.split('');
let temp = str[pos1];
str[pos1] = str[pos2];
str[pos2] = temp;
str = str.join('');
// console.log ('new str', str);
return str;
}

function stringPermutations (str, start, end) {
if (start === end) {
console.log (str);
} else {
for (let i=start; i<end; i++) {
str = swap (str, start, i);
stringPermutations (str, start+1, end);
str = swap (str, i, start);
}
}
}

let inputStr = '123';
stringPermutations (inputStr, 0, inputStr.length);
```

### [Solution by @Karthikeyan](./JavaScript/sol.js)

```js
/*
Expand Down
52 changes: 52 additions & 0 deletions day11/JavaScript/longest_substring_dynamic.js
@@ -0,0 +1,52 @@
/**
* @author MadhavBahlMD
* @date 03/01/2018
* Referance: https://en.wikipedia.org/wiki/Longest_common_substring_problem
*/

function longestSubstring (str1, str2) {
// initialize the longest substring matrix with all zeroes
let strMat = [],
len1 = str1.length,
len2 = str2.length;

for (let i=0; i<=len2; i++) {
let row = [];
for (let j=0; j<=len1; j++) {
row.push(0);
}

strMat.push(row);
}

// Fill the longest substring matrix and find the maximum element simultaneously
let maxi = 0, maxj = 0, max = strMat[0][0];
for (let i=1; i<=len2; i++) {
for (let j=1; j<=len1; j++) {
if (str2[i-1] === str1[j-1]) {
strMat[i][j] = strMat[i-1][j-1] + 1;
if (strMat[i][j] > max) {
max = strMat[i][j];
maxi = i;
maxj = j;
}
}
}
}

// Find the longest substring
let maxSubStr = '';
for (i=maxi, j=maxj; i>=0; i--, j--) {
if (!(i<=0 || j<=0) && strMat[i][j] !== 0) {
maxSubStr += str2[i-1];
} else {
break;
}
}

// Return the reverse of maxSubStr
return maxSubStr.split('').reverse().join('');
}

console.log(longestSubstring ("abcdefg", "xyabcz"));
console.log(longestSubstring ("XYXYXYZ", "XYZYX"));
88 changes: 88 additions & 0 deletions day11/README.md
@@ -0,0 +1,88 @@
![cover](./cover.png)

# Day 11 - Longest Common Substring

**Question** -- Given two strings, write a program to find the longest string that is a substring of both the given strings.

### Example

```
input: str1 = "abcdefg", str2 = "xyabcz"
output: "abc"
input: str1 = "XYXYXYZ", str2 = "XYZYX"
output: "XYZ"
```

### Illustration from [Wikipedia](https://en.wikipedia.org/wiki/Longest_common_substring_problem)

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".

```
ABABC
|||
BABCA
|||
ABCBA
```

## JavaScript Implementation

### [Solution using dynamic programming](./JavaScript/longest_substring_dynamic.js)

**Reference** -- [Wikipedia](https://en.wikipedia.org/wiki/Longest_common_substring_problem#Dynamic_programming)

```js
/**
* @author MadhavBahlMD
* @date 03/01/2018
* Referance: https://en.wikipedia.org/wiki/Longest_common_substring_problem
*/

function longestSubstring (str1, str2) {
// initialize the longest substring matrix with all zeroes
let strMat = [],
len1 = str1.length,
len2 = str2.length;

for (let i=0; i<=len2; i++) {
let row = [];
for (let j=0; j<=len1; j++) {
row.push(0);
}

strMat.push(row);
}

// Fill the longest substring matrix and find the maximum element simultaneously
let maxi = 0, maxj = 0, max = strMat[0][0];
for (let i=1; i<=len2; i++) {
for (let j=1; j<=len1; j++) {
if (str2[i-1] === str1[j-1]) {
strMat[i][j] = strMat[i-1][j-1] + 1;
if (strMat[i][j] > max) {
max = strMat[i][j];
maxi = i;
maxj = j;
}
}
}
}

// Find the longest substring
let maxSubStr = '';
for (i=maxi, j=maxj; i>=0; i--, j--) {
if (!(i<=0 || j<=0) && strMat[i][j] !== 0) {
maxSubStr += str2[i-1];
} else {
break;
}
}

// Return the reverse of maxSubStr
return maxSubStr.split('').reverse().join('');
}

console.log(longestSubstring ("abcdefg", "xyabcz"));
console.log(longestSubstring ("XYXYXYZ", "XYZYX"));
```
Binary file added day11/cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day11/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 50984ea

Please sign in to comment.