-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol-101.clj
40 lines (35 loc) · 1.05 KB
/
sol-101.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
;Levenshtein Distance
;Difficulty: Hard
;Topics: seqs
;Given two sequences x and y, calculate the Levenshtein distance
;of x and y, i. e. the minimum number of edits needed to transform x into y. The allowed edits are:
;- insert a single item
;- delete a single item
;- replace a single item with another item
;WARNING: Some of the test cases may timeout if you write an inefficient solution!
;
; Author: Jitesh Sejwal
;
(fn [s t]
(last
(last
(reduce
(fn [acc x]
(conj
acc
(reduce
(fn [acc2 y]
(let [n (dec (count acc)) m (count acc2)]
(conj
acc2
(if (= x y)
((acc n) (dec m))
(inc
(min
((acc n) (dec m))
(acc2 (dec m))
((acc n) m)))))))
[(count acc)]
s)))
[(vec (range (inc (count s))))]
t))))