forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0072-edit-distance.swift
45 lines (39 loc) · 1.14 KB
/
0072-edit-distance.swift
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
41
42
43
44
45
class Solution {
func minDistance(_ word1: String, _ word2: String) -> Int {
// No operations needed if strings match
if word1 == word2 {
return 0
}
let m = word1.count, n = word2.count
// if one word has no characters, min operations is length of other word
if m == 0 {
return n
}
if n == 0 {
return m
}
var prev = 0
var curr = Array(repeating: 0, count: n + 1)
// convert the strings into an array
let newWord1 = Array(word1)
let newWord2 = Array(word2)
for j in 1...n {
curr[j] = j
}
for i in 1...m {
var prev = curr[0]
curr[0] = i
for j in 1...n {
let temp = curr[j]
if newWord1[i - 1] == newWord2[j - 1] {
curr[j] = prev
}
else {
curr[j] = min(prev, min(curr[j - 1], curr[j])) + 1
}
prev = temp
}
}
return curr[n]
}
}