Skip to content
Merged
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: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ typings/

# Transpiled files
lib/

# IDE
.idea/
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"scripts": {
"lint": "eslint src/**/*.js",
"test": "jest --coverage",
"test-single": "jest --coverage longest-common-substring.spec.js",
"test-debug": "BABEL_ENV=dev node --inspect ./node_modules/.bin/jest --runInBand",
"build": "eslint src/**/*.js && jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && babel src --out-dir lib --ignore spec.js"
},
Expand Down
41 changes: 20 additions & 21 deletions src/longest-common-substring.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* are concatenated with unique markers and their positions recorded in a
* string index map. Iterating over the heights given in the longest common
* prefix array, the string index map is used to lookup whet substring each
* suffix array enry belongs to. If k substrings are seen at the same height,
* suffix array entry belongs to. If k substrings are seen at the same height,
* then a common substring across all given substrings has been found.
*
* Copyright (C) 2017 Kim Burgaard <kim@burgaard.us>
Expand Down Expand Up @@ -288,43 +288,42 @@ export default function longestCommonSubstring(strings, indexMap = 'log') {
const h = lcp[i];
const suffixNext = sa[i + 1];
const stringIndexNext = stringIndexMap.lookup(suffixNext);
let j = 0;
if (h >= top && h > 0) {
// add a stack entry for each height level and map the string index to its suffix and length
while (j < h) {
if (j >= entryStack.length) {
entryStack[j] = {};
}
entryStack[j][stringIndex] = [suffix, j + 1];
entryStack[j][stringIndexNext] = [suffixNext, j + 1];
j++;
}
top = j;
}

// if the height goes down, then check what we have on the stack
j = top;
while (j > h) {
while (top > h) {
const entries = entryStack.pop();
if (j >= longest) {
if (top >= longest) {
const keys = Object.keys(entries);
// check if all k strings have been seen
if (keys.length === k) {
const r = entries[keys[0]];
if (j > longest) {
if (top > longest) {
// reset result for the longest substring seen so far
longest = j;
longest = top;
result = [r];
} else {
// add substring to its peers of longest substrings seen so far
result.push(r);
}
}
}
j--;
top--;
}

let j = 0;
if (h >= top && h > 0) {
// add a stack entry for each height level and map the string index to its suffix and length
while (j < h) {
if (j >= entryStack.length) {
entryStack[j] = {};
}
entryStack[j][stringIndex] = [suffix, j + 1];
entryStack[j][stringIndexNext] = [suffixNext, j + 1];
j++;
}
top = j;
}

top = h;
i++;
suffix = suffixNext;
stringIndex = stringIndexNext;
Expand Down
10 changes: 10 additions & 0 deletions src/longest-common-substring.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,16 @@ describe('longestCommonSubstring', () => {
expect(result).toEqual([]);
});

it('supports use cases related to issue #1', () => {
const result1 = longestCommonSubstring(['abc - 48h', 'abc - 108h', 'abc - 168h']);
const result2 = longestCommonSubstring(['abcTab', 'abcUab', 'abcTab']);
const result3 = longestCommonSubstring(['abcTde', 'abcUde', 'abcTde']);

expect(result1).toEqual(['abc - ']);
expect(result2).toEqual(['abc']);
expect(result3).toEqual(['abc']);
});

it('throws an error when given a boolean', () => {
expect(() => longestCommonSubstring(true)).toThrow();
expect(() => longestCommonSubstring(false)).toThrow();
Expand Down