Skip to content

Implemented longest common subsequence algorithm #45

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 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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"no-param-reassign": "off",
"class-methods-use-this": "off",
"no-bitwise": "off",
"no-continue": "off"
"no-continue": "off",
"linebreak-style": 0
}
}
4 changes: 3 additions & 1 deletion src/algorithms/string/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const levenshteindistance = require('./levenshtein_distance');
const logestcommonsubsequence = require('./longest_common_subsequence');

module.exports = {
levenshteindistance
levenshteindistance,
logestcommonsubsequence
};
66 changes: 66 additions & 0 deletions src/algorithms/string/longest_common_subsequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
function readSolution(dp, firstWord, secondWord, i, j) {
let solution = '';
let value;
let leftCellValue;
let aboveCellValue;
while (i !== 0 && j !== 0) {
value = dp[i][j];
leftCellValue = dp[i][j - 1];
aboveCellValue = dp[i - 1][j];
if (leftCellValue === value) {
j -= 1;
continue;
} else if (aboveCellValue === value) {
i -= 1;
continue;
} else {
solution = firstWord.charAt(i - 1) + solution;
i -= 1;
j -= 1;
}
}
return solution;
}

/**
* Calculates Longest Common Subsequence (LCS) of two numbers
* @param {String} firstWord First string
* @param {String} secondWord Second String
* @return {String} One of the possbile longest common subsequence for given inputs
*
* References: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
*/

function longestcommonsubsequence(firstWord = '', secondWord = '') {
const firstWordSize = firstWord.length;
const secondWordSize = secondWord.length;
if (secondWordSize === 0) {
return '';
}

if (firstWordSize === 0) {
return '';
}
const dp = [...Array(firstWordSize + 1)].map(() => Array(secondWordSize + 1));

for (let i = 0; i <= firstWordSize; i += 1) {
dp[i][0] = 0;
}

for (let i = 0; i <= secondWordSize; i += 1) {
dp[0][i] = 0;
}

for (let i = 1; i <= firstWordSize; i += 1) {
for (let j = 1; j <= secondWordSize; j += 1) {
if (firstWord.charAt(i - 1) === secondWord.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return readSolution(dp, firstWord, secondWord, firstWordSize, secondWordSize);
}

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

describe('LCS', () => {
it('should find OLTA or OLEA for POLITECHNIKA and TOALETA', () => {
const stringA = 'POLITECHNIKA';
const stringB = 'TOALETA';

const result = logestcommonsubsequence(stringB, stringA);
assert.equal(['OLTA', 'OLEA'].indexOf(result) !== -1, true);
});

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

const result = logestcommonsubsequence(stringA, stringB);
assert.equal(result, '3');
});

it('should find baba or abab for abaabbaaa and babab', () => {
const stringA = 'abaabbaaa';
const stringB = 'babab';

const result = logestcommonsubsequence(stringA, stringB);
assert.equal(['baba', 'abab'].indexOf(result) !== -1, true);
});

it('should return empty string when one of inputs is empty', () => {
const stringA = 'abaabbaaa';
const stringB = '';

const distance = logestcommonsubsequence(stringA, stringB);
assert.equal(distance.length, 0);
});

it('should return empty string when both inputs dont have lcs', () => {
const stringA = 'abs';
const stringB = '123';

const distance = logestcommonsubsequence(stringA, stringB);
assert.equal(distance.length, 0);
});
});