Skip to content

Commit

Permalink
Updated Minimum Edit Distance Playground for Swift 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyTheDev committed Oct 31, 2018
1 parent ace62f8 commit 8683fb1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@

// Minimum Edit Distance
// last checked with Xcode 9.0b4
#if swift(>=4.0)
print("Hello, Swift 4!")
#endif

extension String {

public func minimumEditDistance(other: String) -> Int {
let m = self.characters.count
let n = other.characters.count
let m = self.count
let n = other.count
var matrix = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1)

// initialize matrix
Expand All @@ -24,8 +20,8 @@ extension String {
}

// compute Levenshtein distance
for (i, selfChar) in self.characters.enumerated() {
for (j, otherChar) in other.characters.enumerated() {
for (i, selfChar) in self.enumerated() {
for (j, otherChar) in other.enumerated() {
if otherChar == selfChar {
// substitution of equal symbols with cost 0
matrix[i + 1][j + 1] = matrix[i][j]
Expand Down
4 changes: 2 additions & 2 deletions Minimum Edit Distance/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Then in each cell the minimum of the cost of insertion, deletion, or substitutio

```swift
// compute Levenshtein distance
for (i, selfChar) in self.characters.enumerated() {
for (j, otherChar) in other.characters.enumerated() {
for (i, selfChar) in self.enumerated() {
for (j, otherChar) in other.enumerated() {
if otherChar == selfChar {
// substitution of equal symbols with cost 0
matrix[i + 1][j + 1] = matrix[i][j]
Expand Down

0 comments on commit 8683fb1

Please sign in to comment.