Skip to content

Implement longest common substring algorithm #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Various Math algorithms:
Various String algorithms:

- [Levenshtein Distance](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/string/levenshtein_distance.js)
- [Longest Common Substring](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/string/longest_common_substring.js)

#### Geometry
Various Geometry algorithms:
Expand Down
4 changes: 3 additions & 1 deletion src/algorithms/string/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const levenshteindistance = require('./levenshtein_distance');
const longestcommonsubstring = require('./longest_common_substring');

module.exports = {
levenshteindistance
levenshteindistance,
longestcommonsubstring
};
40 changes: 40 additions & 0 deletions src/algorithms/string/longest_common_substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Returns Longest Common Substring of two strings
* @param {String} firstWord First string
* @param {String} secondWord Second string
* @return {String} One of the possbile longest common substring for given inputs
*/
function longestcommonsubstring(firstWord = '', secondWord = '') {
const firstWordSize = firstWord.length;
const secondWordSize = secondWord.length;
const table = [...Array(firstWordSize + 1)].map(() => Array(secondWordSize + 1));
let maxSubstringLength = 0;
let maxSubstringRow = 0;
let maxSubstringCol = 0;
for (let i = 0; i <= firstWordSize; i += 1) {
for (let j = 0; j <= secondWordSize; j += 1) {
if (i === 0 || j === 0) {
table[i][j] = 0;
} else if (firstWord.charAt(i - 1) === secondWord.charAt(j - 1)) {
table[i][j] = table[i - 1][j - 1] + 1;
if (table[i][j] > maxSubstringLength) {
maxSubstringLength = table[i][j];
maxSubstringRow = i;
maxSubstringCol = j;
}
} else {
table[i][j] = 0;
}
}
}

let result = '';
while (table[maxSubstringRow][maxSubstringCol] !== 0) {
result = firstWord.charAt(maxSubstringRow - 1) + result;
maxSubstringRow -= 1;
maxSubstringCol -= 1;
}
return result;
}

module.exports = longestcommonsubstring;
46 changes: 46 additions & 0 deletions test/algorithms/string/testLongestCommonSubstring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-env mocha */
const longestcommonsubstring = require('../../../src').algorithms.string.longestcommonsubstring;
const assert = require('assert');

describe('Longest Common Substring', () => {
it('should find abcd for xyzabcd and abcdxyz', () => {
const stringA = 'xyzabcd';
const stringB = 'abcdxyz';

const result = longestcommonsubstring(stringA, stringB);
assert.equal(result, 'abcd');
});

it('should find Geeks for GeeksforGeeks and GeeksQuiz', () => {
const stringA = 'GeeksForGeeks';
const stringB = 'GeeksQuiz';

const result = longestcommonsubstring(stringA, stringB);
assert.equal(result, 'Geeks');
});

it('should return empty string when one or both of inputs is empty', () => {
const resultOne = longestcommonsubstring('abcdef', '');
assert.equal(resultOne, '');
const resultTwo = longestcommonsubstring('', 'abcasds');
assert.equal(resultTwo, '');
const resultThree = longestcommonsubstring('', '');
assert.equal(resultThree, '');
});

it('should return empty string for letter and aggghhhww', () => {
const stringA = 'letter';
const stringB = 'aggghhhww';

const result = longestcommonsubstring(stringA, stringB);
assert.equal(result, '');
});

it('should return a for xysysysyysahhfghf and aaaaaa', () => {
const stringA = 'xysysysyysahhfghf';
const stringB = 'aaaaaa';

const result = longestcommonsubstring(stringA, stringB);
assert.equal(result, 'a');
});
});