Debug longest common substring
by Gabe Rogan
JavaScript
// Define strings
s1 = 'dadxx'
s2 = 'ddxx'
matchChars(s1,s2)
matchChars(s2,s1)
function matchChars(a,b) {
// Convert strings to lower case for case insensitive matching
// Remove if case sensitive matching required
a = a.toLowerCase();
b = b.toLowerCase();
// Iterate through every letter in s1
for (i = 0; i < a.length; i++) {
// Iterate through every letter in s2
for (j = 0; j < b.length; j++) {
// Check if the letter in s1 matches letter in s2
if (a[i] === b[j]) {
if (a === s1) console.log(i, j);
else console.log(j, i, 'asdf')
}
}
}
}
/*
NOTES:
NATIVE STRING METHODS IGNORE SPACES (but it still works)
Think about the brute force method
Find ALL 2-long subseq, store them, see which ones have 3-long, then 4, 5, etc.
but try for 1-long first (do they both have an a,b,c,etc.)
but first create a char list of all the chars in each string.
OLD CODE:
// Helper: filter duplicates
function filterDuplicates(array) {
var newArray = []
for(var i=0;i<array.length;i++) {
// If newArray doesn't contain the thing in old array
if (indexOf(newArray,array[i]) == -1) newArray.push(array[i])
}
function indexOf(array_,element) {
for(var i=0;i<array_.length;i++) {
if (array_[i].toString() == element.toString()) return i
}
return -1
}
return newArray
}
// The last part
matches[1].forEach(function(match) {
var pos1 = match[0], pos2 = match[1], length = match[2]
if (s1[pos1 + 1] === s2[pos2 + 1]) addMatch(pos1,pos2,2,true)
}) */