diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..d4288847 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 2f0dd3cf..af679f82 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /.nyc_output /npm-debug.log dist +**/.DS_Store diff --git a/package-lock.json b/package-lock.json index af75e273..b64d1db0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -483,6 +483,7 @@ "resolved": "https://registry.npmjs.org/align-text/-/align-text-0.1.4.tgz", "integrity": "sha1-DNkKVhCT810KmSVsIrcGlDP60Rc=", "dev": true, + "optional": true, "requires": { "kind-of": "^3.0.2", "longest": "^1.0.1", @@ -3854,7 +3855,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -3875,12 +3877,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3895,17 +3899,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -4022,7 +4029,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -4034,6 +4042,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -4048,6 +4057,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -4055,12 +4065,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -4079,6 +4091,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -4159,7 +4172,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -4171,6 +4185,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -4256,7 +4271,8 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -4292,6 +4308,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -4311,6 +4328,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4354,12 +4372,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -5193,7 +5213,8 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz", "integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=", - "dev": true + "dev": true, + "optional": true }, "loose-envify": { "version": "1.4.0", @@ -5640,6 +5661,7 @@ "version": "0.1.4", "bundled": true, "dev": true, + "optional": true, "requires": { "kind-of": "^3.0.2", "longest": "^1.0.1", @@ -5961,7 +5983,8 @@ "is-buffer": { "version": "1.1.6", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "is-builtin-module": { "version": "1.0.0", @@ -6045,6 +6068,7 @@ "version": "3.2.2", "bundled": true, "dev": true, + "optional": true, "requires": { "is-buffer": "^1.1.5" } @@ -6091,7 +6115,8 @@ "longest": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "lru-cache": { "version": "4.1.3", @@ -6357,7 +6382,8 @@ "repeat-string": { "version": "1.6.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "require-directory": { "version": "2.1.1", diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 00000000..a3326275 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/algorithms/.DS_Store b/src/algorithms/.DS_Store new file mode 100644 index 00000000..7185f355 Binary files /dev/null and b/src/algorithms/.DS_Store differ diff --git a/src/algorithms/string/.DS_Store b/src/algorithms/string/.DS_Store new file mode 100644 index 00000000..ad874d49 Binary files /dev/null and b/src/algorithms/string/.DS_Store differ diff --git a/src/algorithms/string/LongestCommLeadtrailSubString.js b/src/algorithms/string/LongestCommLeadtrailSubString.js new file mode 100644 index 00000000..1d17a36d --- /dev/null +++ b/src/algorithms/string/LongestCommLeadtrailSubString.js @@ -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; \ No newline at end of file diff --git a/test/.DS_Store b/test/.DS_Store new file mode 100644 index 00000000..d9c3af28 Binary files /dev/null and b/test/.DS_Store differ diff --git a/test/algorithms/.DS_Store b/test/algorithms/.DS_Store new file mode 100644 index 00000000..30b048b2 Binary files /dev/null and b/test/algorithms/.DS_Store differ diff --git a/test/algorithms/string/testleadtrail.js b/test/algorithms/string/testleadtrail.js new file mode 100644 index 00000000..47da7230 --- /dev/null +++ b/test/algorithms/string/testleadtrail.js @@ -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"}); + }); +});