Skip to content

Instantly share code, notes, and snippets.

@slifin
Created October 6, 2021 19:02
Show Gist options
  • Save slifin/a0a06041378f2252679d06e57a01c819 to your computer and use it in GitHub Desktop.
Save slifin/a0a06041378f2252679d06e57a01c819 to your computer and use it in GitHub Desktop.
Detects if two strings are one edit away
(ns slifin.tdd2
(:require [hyperfiddle.rcf :refer [tests]]))
(defn one-edit-away? [a b]
(cond (= a b) true
(> (Math/abs (- (count a) (count b))) 1) false
(= (first a) (first b)) (recur (subs a 1) (subs b 1))
(and (= (count a) (count b))
(= a (str (first a) (subs b 1)))) true
(and (> (count a) (count b))
(= a (str (first a) b))) true
(and (< (count a) (count b))
(= a (subs b 1))) true
:else false))
(tests
(one-edit-away? "" "") := true
(one-edit-away? "a" "") := true
(one-edit-away? "" "a") := true
(one-edit-away? "aa" "") := false
(one-edit-away? "" "aa") := false
(one-edit-away? "rasp" "rap") := true
(one-edit-away? "aa" "aaa") := true
(one-edit-away? "snap" "snip") := true
(one-edit-away? "sni" "snap") := false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment