Skip to content

Instantly share code, notes, and snippets.

@ohnishiakira
Last active December 19, 2015 18:28
Show Gist options
  • Save ohnishiakira/5998674 to your computer and use it in GitHub Desktop.
Save ohnishiakira/5998674 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# coding: utf-8
def levenshtein(a, b)
(0..a.size).inject([[*0..b.size+1]]){|d, i|
d << (0..b.size).inject([i+1]){|_d, j|
_d << [
d[i][j+1] + 1,
_d[j] + 1,
d[i][j] + (a[i]==b[j] ?0:1)
].min
}
}[-1][-1]
end
puts levenshtein(ARGV[0], ARGV[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment