Skip to content

Implement longest common leading/trailing substring algorithms #116

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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Various Math algorithms:
Various String algorithms:

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

#### Geometry
Various Geometry algorithms:
Expand Down
6 changes: 5 additions & 1 deletion src/algorithms/string/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const levenshteindistance = require('./levenshtein_distance');
const longestcommonleadingsubstring = require('./longest_common_leading_substring');
const longestcommontrailingsubstring = require('./longest_common_trailing_substring');

module.exports = {
levenshteindistance
levenshteindistance,
longestcommonleadingsubstring,
longestcommontrailingsubstring
};
20 changes: 20 additions & 0 deletions src/algorithms/string/longest_common_leading_substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Returns Longest Common Leading Substring of two strings
* @param {String} firstWord First string
* @param {String} secondWord Second string
* @return {String} Longest common leading substring for given inputs
*/
function longestcommonleadingsubstring(word1 = '', word2 = '') {
const end1 = word1.length - 1;
const end2 = word2.length - 1;
let pos = 0;
while (pos <= end1 && pos <= end2) {
if (word1[pos] !== word2[pos]) {
return word1.substring(0, pos);
}
pos += 1;
}
return word1.substring(0, pos);
}

module.exports = longestcommonleadingsubstring;
21 changes: 21 additions & 0 deletions src/algorithms/string/longest_common_trailing_substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Returns Longest Common Trailing Substring of two strings
* @param {String} firstWord First string
* @param {String} secondWord Second string
* @return {String} Longest common trailing substring for given inputs
*/
function longestcommontrailingsubstring(word1 = '', word2 = '') {
let pos1 = word1.length - 1;
let pos2 = word2.length - 1;

while (pos1 >= 0 && pos2 >= 0) {
if (word1[pos1] !== word2[pos2]) {
return word1.substring(pos1 + 1);
}
pos1 -= 1;
pos2 -= 1;
}
return word1.substring(pos1 + 1);
}

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

describe('Longest common leading substring', () => {
it('should find bb for bbaaxyz and bb', () => {
const stringA = 'bbaaxyz';
const stringB = 'bb';

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

it('should find abab for ababxyz and ababc', () => {
const stringA = 'ababxyz';
const stringB = 'ababc';

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

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

it('should find u for u and uahhfghfgdfhytyty', () => {
const stringA = 'u';
const stringB = 'uahhfghfgdfhytyty';

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

it('should find x for x and x', () => {
const stringA = 'x';
const stringB = 'x';

const result = longestcommonleadingsubstring(stringA, stringB);
assert.equal(result, 'x');
});
});
46 changes: 46 additions & 0 deletions test/algorithms/string/testLongestCommonTrailingSubstring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-env mocha */
const longestcommontrailingsubstring = require('../../../src').algorithms.string.longestcommontrailingsubstring;
const assert = require('assert');

describe('Longest common trailing substring', () => {
it('should find bb for xyzaabb and bb', () => {
const stringA = 'xyzaabb';
const stringB = 'bb';

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

it('should find abab for xyzabab and cabab', () => {
const stringA = 'xyzabab';
const stringB = 'cabab';

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

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

it('should find u for u and ahhfghfgdfhytytyu', () => {
const stringA = 'u';
const stringB = 'ahhfghfgdfhytytyu';

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

it('should find x for x and x', () => {
const stringA = 'x';
const stringB = 'x';

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