File tree Expand file tree Collapse file tree 2 files changed +10
-5
lines changed
Minimum Edit Distance/MinimumEditDistance.playground Expand file tree Collapse file tree 2 files changed +10
-5
lines changed Original file line number Diff line number Diff line change 1
- extension String {
1
+ // last checked with Xcode 9.0b4
2
+ #if swift(>=4.0)
3
+ print ( " Hello, Swift 4! " )
4
+ #endif
2
5
6
+ extension String {
7
+
3
8
public func minimumEditDistance( other: String ) -> Int {
4
9
let m = self . characters. count
5
10
let n = other. characters. count
6
11
var matrix = [ [ Int] ] ( repeating: [ Int] ( repeating: 0 , count: n + 1 ) , count: m + 1 )
7
-
12
+
8
13
// initialize matrix
9
14
for index in 1 ... m {
10
15
// the distance of any first string to an empty second string
11
16
matrix [ index] [ 0 ] = index
12
17
}
13
-
18
+
14
19
for index in 1 ... n {
15
20
// the distance of any second string to an empty first string
16
21
matrix [ 0 ] [ index] = index
17
22
}
18
-
23
+
19
24
// compute Levenshtein distance
20
25
for (i, selfChar) in self . characters. enumerated ( ) {
21
26
for (j, otherChar) in other. characters. enumerated ( ) {
@@ -25,7 +30,7 @@ extension String {
25
30
} else {
26
31
// minimum of the cost of insertion, deletion, or substitution
27
32
// 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 )
29
34
}
30
35
}
31
36
}
You can’t perform that action at this time.
0 commit comments