Skip to content

Instantly share code, notes, and snippets.

Created June 2, 2011 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1005512 to your computer and use it in GitHub Desktop.
Save anonymous/1005512 to your computer and use it in GitHub Desktop.
;; lucas1000001's solution to Levenshtein Distance
;; https://4clojure.com/problem/101
(fn [s1 s2]
(letfn [(c [x y] (if (= (nth s1 (dec x)) (nth s2 (dec y))) 0 1))]
(with-local-vars
[l (memoize
(fn [x y] (cond (< x 1) y
(< y 1) x
:else
(let [mx (dec x) my (dec y)]
(min (+ 1 (l mx y))
(+ 1 (l x my))
(+ (c x y) (l mx my)))))))]
(l (count s1) (count s2)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment