Skip to content

Instantly share code, notes, and snippets.

@tomwans
Created June 20, 2011 23:32
Show Gist options
  • Save tomwans/1036869 to your computer and use it in GitHub Desktop.
Save tomwans/1036869 to your computer and use it in GitHub Desktop.
Editor =
(str1, str2) ->
l1 = str1.length
l2 = str2.length
return Math.max l1, l2 if Math.min(l1, l2) == 0
i = 0; j = 0; distance = []
for i in [0...l1 + 1]
distance[i] = []
distance[i][0] = i
distance[0][j] = j for j in [0...l2 + 1]
#console.log "dist1", distance
for i in [1...l1 + 1]
for j in [1...l2 + 1]
distance[i][j] = Math.min distance[i - 1][j] + 1,
distance[i][j - 1] + 1,
distance[i - 1][j - 1] + `(str1.charAt(i - 1) === str2.charAt(j - 1) ? 0 : 1)`
#console.log "dist2", distance
distance[l1][l2]
alert Editor "kitten", "sitting"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment