Skip to content

Commit 99d955a

Browse files
committed
Minimum Edit Distance updated to Swift 4
1 parent 0e0ae64 commit 99d955a

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Minimum Edit Distance/MinimumEditDistance.playground/Contents.swift

100755100644
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
extension String {
1+
// last checked with Xcode 9.0b4
2+
#if swift(>=4.0)
3+
print("Hello, Swift 4!")
4+
#endif
25

6+
extension String {
7+
38
public func minimumEditDistance(other: String) -> Int {
49
let m = self.characters.count
510
let n = other.characters.count
611
var matrix = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1)
7-
12+
813
// initialize matrix
914
for index in 1...m {
1015
// the distance of any first string to an empty second string
1116
matrix[index][0] = index
1217
}
13-
18+
1419
for index in 1...n {
1520
// the distance of any second string to an empty first string
1621
matrix[0][index] = index
1722
}
18-
23+
1924
// compute Levenshtein distance
2025
for (i, selfChar) in self.characters.enumerated() {
2126
for (j, otherChar) in other.characters.enumerated() {
@@ -25,7 +30,7 @@ extension String {
2530
} else {
2631
// minimum of the cost of insertion, deletion, or substitution
2732
// added to the already computed costs in the corresponding cells
28-
matrix[i + 1][j + 1] = min(matrix[i][j] + 1, matrix[i + 1][j] + 1, matrix[i][j + 1] + 1)
33+
matrix[i + 1][j + 1] = Swift.min(matrix[i][j] + 1, matrix[i + 1][j] + 1, matrix[i][j + 1] + 1)
2934
}
3035
}
3136
}

Minimum Edit Distance/MinimumEditDistance.playground/contents.xcplayground

100755100644
File mode changed.

0 commit comments

Comments
 (0)