Skip to content

Instantly share code, notes, and snippets.

@daniilS
Created January 26, 2018 15:12
Show Gist options
  • Save daniilS/f1bfee9669d1f69182d8a541f1afcfca to your computer and use it in GitHub Desktop.
Save daniilS/f1bfee9669d1f69182d8a541f1afcfca to your computer and use it in GitHub Desktop.
Multiply and surrender
//helper function
String.prototype.cut = function(index){
return this.slice(0, index) + this.slice(index+1);
}
function longestCommonSubstring(a, b){
if(a.length == 0 || b.length == 0)
return 0;
if(a == b)
return a.length;
let longest = 0;
for(let i = 0; i < a.length; i++)
longest = Math.max(longest, longestCommonSubstring(a.cut(i), b));
for(let i = 0; i < b.length; i++)
longest = Math.max(longest, longestCommonSubstring(a, b.cut(i)));
return longest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment