Skip to content

Commit

Permalink
Merge b1ea461 into 2211ce9
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Curl committed Sep 1, 2014
2 parents 2211ce9 + b1ea461 commit 1b3e2fb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
8 changes: 3 additions & 5 deletions algorithms/string/longest_common_subsequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ var longestCommonSubsequence = function (s1, s2) {
for (i = 1; i < s1.length + 1; i++) {
for (j = 1; j < s2.length + 1; j++) {
if (s1[i - 1] == s2[j - 1]) {
var newValue = cache[i - 1][j - 1] + 1;
cache[i][j] = newValue;
cache[i][j] = cache[i - 1][j - 1] + 1;
}
else {
cache[i][j] = Math.max(cache[i][j - 1], cache[i - 1][j]);
Expand All @@ -44,7 +43,7 @@ var longestCommonSubsequence = function (s1, s2) {

while (cache[i][j] !== 0) {
if (s1[i - 1] === s2[j - 1]) {
lcs += s1[i - 1];
lcs = s1[i - 1] + lcs;
i--;
j--;
}
Expand All @@ -58,8 +57,7 @@ var longestCommonSubsequence = function (s1, s2) {
}
}

// LCS is built in reverse, return reversed string
return lcs.split('').reverse().join('');
return lcs;
};

module.exports = longestCommonSubsequence;
71 changes: 71 additions & 0 deletions algorithms/string/longest_common_substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Implementation of longest common substring
*
* @author Joshua Curl <curljosh@msu.edu>
*/

'use strict';

/**
* Implementation via dynamic programming
*/
var longestCommonSubstring = function (s1, s2) {
// Multidimensional array for dynamic programming algorithm
var cache = [];

var i, j;

// First column and row are initialized with zeroes
for (i = 0; i < s1.length + 1; i++) {
cache[i] = [];
cache[i][0] = 0;
}
for (i = 0; i < s2.length + 1; i++) {
cache[0][i] = 0;
}

var lcsPosition = {};
var commonSubstringFound = false;

// Fill in the cache
for (i = 1; i < s1.length + 1; i++) {
for (j = 1; j < s2.length + 1; j++) {
if (s1[i - 1] == s2[j - 1]) {
cache[i][j] = cache[i - 1][j - 1] + 1;
if (commonSubstringFound) {
if (cache[i][j] > cache[lcsPosition.i][lcsPosition.j]) {
lcsPosition.i = i;
lcsPosition.j = j;
}
}
else {
lcsPosition.i = i;
lcsPosition.j = j;
commonSubstringFound = true;
}
}
else {
cache[i][j] = 0;
}
}
}

if (!commonSubstringFound) {
return '';
}

// Build LCS from cache
i = lcsPosition.i;
j = lcsPosition.j;
var lcs = '';

while (cache[i][j] !== 0) {
lcs = s1[i - 1] + lcs;
i--;
j--;
}

return lcs;
};

module.exports = longestCommonSubstring;
5 changes: 3 additions & 2 deletions test/algorithms/string/longest_common_subseqence.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

var directory = '../../../algorithms/string/';
var filename = 'longest_common_subsequence';
var longestCommonSubsequence = require([directory, filename].join('')),
var longestCommonSubsequence = require(directory + filename),
assert = require('assert');

describe('Longest common subsequence', function () {
it('should return the proper longest common subsequence', function () {
it('should return the longest common subsequence of ' +
'two strings', function () {
assert.equal('', longestCommonSubsequence('', ''));
assert.equal('', longestCommonSubsequence('', 'aaa'));
assert.equal('', longestCommonSubsequence('aaa', ''));
Expand Down
18 changes: 18 additions & 0 deletions test/algorithms/string/longest_common_substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var directory = '../../../algorithms/string/';
var filename = 'longest_common_substring';
var longestCommonSubstring = require(directory + filename),
assert = require('assert');

describe('Longest common substring', function () {
it('should return the longest common substring of two strings', function () {
assert.equal('', longestCommonSubstring('', ''));
assert.equal('', longestCommonSubstring('', 'aaa'));
assert.equal('', longestCommonSubstring('aaa', ''));
assert.equal('aaa', longestCommonSubstring('aaa', 'aaa'));
assert.equal('xun', longestCommonSubstring('xunmjyauz', 'mzjawxun'));
assert.equal('xun', longestCommonSubstring('jyaxunmuz', 'mzjawxun'));
assert.equal('xun', longestCommonSubstring('jyamuzxun', 'mzjawxun'));
});
});

0 comments on commit 1b3e2fb

Please sign in to comment.