You should find the longest common substring .
If the lines are not very long, I recommend using Tim's approach. Otherwise, it is a Javascript implementation of the longest general substring algorithm with dynamic programming. The runtime is O (mn), where m and n are the lengths of two lines, respectively.
Usage example:
var first = "Here is a quick guide for the next time you reach for your favorite oil and some other topics"; var second = "favorite oil and some other topics can be based on something blah blah"; console.log(first.intersection(second));
This is an implementation of the algorithm. It returns an array of the longest common substring. Made its own String class, so the intersect method is available for all strings.
String.prototype.intersection = function(anotherString) { var grid = createGrid(this.length, anotherString.length); var longestSoFar = 0; var matches = []; for(var i = 0; i < this.length; i++) { for(var j = 0; j < anotherString.length; j++) { if(this.charAt(i) == anotherString.charAt(j)) { if(i == 0 || j == 0) { grid[i][j] = 1; } else { grid[i][j] = grid[i-1][j-1] + 1; } if(grid[i][j] > longestSoFar) { longestSoFar = grid[i][j]; matches = []; } if(grid[i][j] == longestSoFar) { var match = this.substring(i - longestSoFar + 1, i); matches.push(match); } } } } return matches; }
You also need this helper function to create a 2d array with all initialized elements up to 0.
// create a 2d array function createGrid(rows, columns) { var grid = new Array(rows); for(var i = 0; i < rows; i++) { grid[i] = new Array(columns); for(var j = 0; j < columns; j++) { grid[i][j] = 0; } } return grid; }
Anurag
source share