Skip to content

Commit

Permalink
Merge pull request #926 from mick31/lcs/update_depricated_string_access
Browse files Browse the repository at this point in the history
fix: updated LCS string extension to access string directly
  • Loading branch information
kelvinlauKL authored Aug 29, 2020
2 parents a3b45fc + 4735644 commit 6de22fc
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// last checked with Xcode 9.0b4
// last checked with Xcode 11.4
#if swift(>=4.0)
print("Hello, Swift 4!")
#endif
Expand All @@ -7,10 +7,10 @@ extension String {
public func longestCommonSubsequence(_ other: String) -> String {

func lcsLength(_ other: String) -> [[Int]] {
var matrix = [[Int]](repeating: [Int](repeating: 0, count: other.characters.count+1), count: self.characters.count+1)
var matrix = [[Int]](repeating: [Int](repeating: 0, count: other.count+1), count: self.count+1)

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 {
matrix[i+1][j+1] = matrix[i][j] + 1
} else {
Expand All @@ -22,8 +22,8 @@ extension String {
}

func backtrack(_ matrix: [[Int]]) -> String {
var i = self.characters.count
var j = other.characters.count
var i = self.count
var j = other.count
var charInSequence = self.endIndex

var lcs = String()
Expand All @@ -41,7 +41,7 @@ extension String {
lcs.append(self[charInSequence])
}
}
return String(lcs.characters.reversed())
return String(lcs.reversed())
}

return backtrack(lcsLength(other))
Expand Down

0 comments on commit 6de22fc

Please sign in to comment.