Last active
August 29, 2015 14:06
-
-
Save crimx/a81c149ac98a5de2b14e to your computer and use it in GitHub Desktop.
Bottom-up DP for longest common substring (not subsequence). http://jsperf.com/crimx-lcstr/2
This file contains hidden or 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
/** | |
* Bottom-up DP for longest common substring (not subsequence). | |
* Time: O(m*n) | |
* Space: O(min(m,n)) | |
* @author Jesse Wong (@straybugs) | |
*/ | |
function lcstr(sstr, lstr) { | |
'use strict'; | |
if (typeof(sstr) !== 'string' || typeof(lstr) !== 'string') { | |
return ''; | |
} | |
// sstr should be shorter | |
if (sstr.length > lstr.length) { | |
sstr = [lstr, lstr = sstr][0]; | |
} | |
var slen = sstr.length | |
, llen = lstr.length | |
, memo = [] | |
, maxValue = 0 | |
, maxIndex = 0 | |
, i, j, k | |
; | |
// note the "<=", leave memo[0] to lstr. | |
for (i = 0; i <= slen; i += 1) { | |
memo[i] = 0; | |
} | |
for (i = 0; i < llen; i += 1) { | |
// must traverse backward | |
for (j = slen-1; j >= 0; j -= 1) { | |
// remember the memo got 1 offset | |
k = j+1; | |
if (lstr.charAt(i) === sstr.charAt(j)) { | |
memo[k] = memo[j] + 1; | |
if (maxValue < memo[k]) { | |
maxValue = memo[k]; | |
maxIndex = k; | |
} | |
} else { | |
memo[k] = 0; | |
} | |
} | |
} | |
return sstr.slice(maxIndex-maxValue, maxIndex); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment