diff --git a/README.md b/README.md index ce93985d..5aa92e63 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/algorithms/string/index.js b/src/algorithms/string/index.js index ad535890..b5480db1 100644 --- a/src/algorithms/string/index.js +++ b/src/algorithms/string/index.js @@ -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 }; diff --git a/src/algorithms/string/longest_common_leading_substring.js b/src/algorithms/string/longest_common_leading_substring.js new file mode 100644 index 00000000..1631c03a --- /dev/null +++ b/src/algorithms/string/longest_common_leading_substring.js @@ -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; diff --git a/src/algorithms/string/longest_common_trailing_substring.js b/src/algorithms/string/longest_common_trailing_substring.js new file mode 100644 index 00000000..281b4c8d --- /dev/null +++ b/src/algorithms/string/longest_common_trailing_substring.js @@ -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; diff --git a/test/algorithms/string/testLongestCommonLeadingSubstring.js b/test/algorithms/string/testLongestCommonLeadingSubstring.js new file mode 100644 index 00000000..5ce3733c --- /dev/null +++ b/test/algorithms/string/testLongestCommonLeadingSubstring.js @@ -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'); + }); +}); diff --git a/test/algorithms/string/testLongestCommonTrailingSubstring.js b/test/algorithms/string/testLongestCommonTrailingSubstring.js new file mode 100644 index 00000000..71490935 --- /dev/null +++ b/test/algorithms/string/testLongestCommonTrailingSubstring.js @@ -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'); + }); +});