Skip to content
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

Adding Program to compute Longest Common Leading and Trailing Substring with tests cases #120

Open
wants to merge 4 commits 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
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.nyc_output
/npm-debug.log
dist
**/.DS_Store
56 changes: 41 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/algorithms/.DS_Store
Binary file not shown.
Binary file added src/algorithms/string/.DS_Store
Binary file not shown.
45 changes: 45 additions & 0 deletions src/algorithms/string/LongestCommLeadtrailSubString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Calculates Longest common leading and trailing substring of two numbers
* @param {Number} a First string
* @param {Number} b Second string
* @return {Number} Longest common leading/trailing substring
*/
const LongestCommLeadtrailSubString = (str1, str2) => {
var leading = "";
var trailing = "";
var leadflag = false;
var trailflag = false;
if(str1.length ===0 || str2.length === 0){
console.log("Must enter valid strings")
}

var end1 = str1.length - 1
var end2 = str2.length - 1

let pos = 0

while (pos <= end1 && pos <= end2 && (leadflag === false || trailflag === false)) {
if (leadflag === false) {
if (str1[pos] === str2[pos]) {
leading = str1.substring(0, pos+1)

}
else{
leadflag = true
}
}
if (trailflag === false) {
if (str1[end1 - pos] === str2[end2 - pos]) {
trailing = str1.substring(end1 - pos)

}
else{
trailflag = true;
}
}
pos++
}
return {"leading":leading,"trailing": trailing}
};

module.exports = LongestCommLeadtrailSubString;
Binary file added test/.DS_Store
Binary file not shown.
Binary file added test/algorithms/.DS_Store
Binary file not shown.
38 changes: 38 additions & 0 deletions test/algorithms/string/testleadtrail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-env mocha */
const LongestCommLeadtrailSubString = require('../../../src/algorithms/string/LongestCommLeadtrailSubString');
const expect = require('expect');


describe('Longest common leading and trailing substring of two strings', () => {
it('should return empty strings if the inputs mismatch entirely', () => {
const stringA = 'ABCDEF';
const stringB = 'QRST';

const SubStrings = LongestCommLeadtrailSubString(stringA, stringB);
expect(SubStrings).toStrictEqual({"leading":"","trailing": ""});
});

it('should return only trailing common substring if no leading common subtring found', () => {
const stringA = 'ABCMNOP';
const stringB = 'EFGHMNOP';

const SubStrings = LongestCommLeadtrailSubString(stringA, stringB);
expect(SubStrings).toStrictEqual({"leading":"","trailing": "MNOP"});
});

it('should return only leading common substring if no trailing common subtring found', () => {
const stringA = 'ABCMNOP';
const stringB = 'ABCYZ';

const SubStrings = LongestCommLeadtrailSubString(stringA, stringB);
expect(SubStrings).toStrictEqual({"leading":"ABC","trailing": ""});
});

it('should return both leading common substring and trailing common subtring if found', () => {
const stringA = 'ABCYEFG';
const stringB = 'ABCZEFG';

const SubStrings = LongestCommLeadtrailSubString(stringA, stringB);
expect(SubStrings).toStrictEqual({"leading":"ABC","trailing": "EFG"});
});
});