Last active
December 17, 2015 05:47
-
-
Save lykkin/456efa631917a0543d25 to your computer and use it in GitHub Desktop.
diff stuff
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
function createMatrix(m, n) { | |
var res = [] | |
for (var i = 0; i < m; i++){ | |
var row = [] | |
for (var j = 0; j < n; j++) { | |
row.push({ | |
length: 0, | |
sequences: [] | |
}) | |
} | |
res.push(row) | |
} | |
return res | |
} | |
function copySequences(source, dest) { | |
for(var i = 0; i < source.length; i++) { | |
var copy = [] | |
for (var j = 0; j < source[i].length; j++) { | |
copy.push(source[i][j]) | |
} | |
dest.push(copy) | |
} | |
} | |
function longestCommonSubsequence(str1, str2) { | |
var str1List = str1.split('') | |
var str2List = str2.split('') | |
var matrix = createMatrix(str1List.length + 1, str2List.length + 1) | |
for (var i = 0; i < str1List.length; i++) { | |
for (var j = 0; j < str2List.length; j++) { | |
if (str1List[i] === str2List[j]) { | |
matrix[i + 1][j + 1].length = 1 + matrix[i][j].length | |
if (matrix[i][j].sequences.length > 0) { | |
copySequences(matrix[i][j].sequences, matrix[i + 1][j + 1].sequences) | |
} else { | |
matrix[i + 1][j + 1].sequences.push([]) | |
} | |
for (var k = 0; k < matrix[i + 1][j + 1].sequences.length; k++) { | |
matrix[i + 1][j + 1].sequences[k].push(str1List[i]) | |
} | |
continue | |
} | |
if (matrix[i][j + 1].length > matrix[i + 1][j].length) { | |
matrix[i + 1][j + 1].length = matrix[i][j + 1].length | |
copySequences(matrix[i][j + 1].sequences, matrix[i + 1][j + 1].sequences) | |
continue | |
} | |
if (matrix[i][j + 1].length < matrix[i + 1][j].length) { | |
copySequences(matrix[i + 1][j].sequences, matrix[i + 1][j + 1].sequences) | |
matrix[i + 1][j + 1].length = matrix[i + 1][j].length | |
continue | |
} | |
copySequences(matrix[i][j + 1].sequences, matrix[i + 1][j + 1].sequences) | |
copySequences(matrix[i + 1][j].sequences, matrix[i + 1][j + 1].sequences) | |
matrix[i + 1][j + 1].length = matrix[i][j + 1].length | |
} | |
} | |
return matrix[str1List.length][str2List.length] | |
} | |
function diff(s1, s2) { | |
var lcs = longestCommonSubsequence(s1, s2) | |
if (lcs.sequences.length === 0) { | |
return [s1, s2] | |
} | |
var res1 = '' | |
var res2 = '' | |
var index = 0 | |
var i = 0 | |
var sequence = lcs.sequences[0] | |
for (i = 0; i < s1.length; i++) { | |
if (sequence[index] === s1[i]) { | |
if (++index === sequence.length) { | |
res1 += s1.slice(i + 1) | |
break | |
} | |
} else { | |
res1 += s1[i] | |
} | |
} | |
index = 0 | |
for (i = 0; i < s2.length; i++) { | |
if (sequence[index] === s2[i]) { | |
if (++index === sequence.length) { | |
res2 += s2.slice(i + 1) | |
break | |
} | |
} else { | |
res2 += s2[i] | |
} | |
} | |
return [res1, res2] | |
} | |
console.log(diff('asdfasdf','asf')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment