Created
January 26, 2018 15:12
-
-
Save daniilS/f1bfee9669d1f69182d8a541f1afcfca to your computer and use it in GitHub Desktop.
Multiply and surrender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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