1
- // last checked with Xcode 9.0b4
1
+ // last checked with Xcode 11.4
2
2
#if swift(>=4.0)
3
3
print ( " Hello, Swift 4! " )
4
4
#endif
@@ -7,10 +7,10 @@ extension String {
7
7
public func longestCommonSubsequence( _ other: String ) -> String {
8
8
9
9
func lcsLength( _ other: String ) -> [ [ Int ] ] {
10
- var matrix = [ [ Int] ] ( repeating: [ Int] ( repeating: 0 , count: other. characters . count+ 1 ) , count: self . characters . count+ 1 )
10
+ var matrix = [ [ Int] ] ( repeating: [ Int] ( repeating: 0 , count: other. count+ 1 ) , count: self . count+ 1 )
11
11
12
- for (i, selfChar) in self . characters . enumerated ( ) {
13
- for (j, otherChar) in other. characters . enumerated ( ) {
12
+ for (i, selfChar) in self . enumerated ( ) {
13
+ for (j, otherChar) in other. enumerated ( ) {
14
14
if otherChar == selfChar {
15
15
matrix [ i+ 1 ] [ j+ 1 ] = matrix [ i] [ j] + 1
16
16
} else {
@@ -22,8 +22,8 @@ extension String {
22
22
}
23
23
24
24
func backtrack( _ matrix: [ [ Int ] ] ) -> String {
25
- var i = self . characters . count
26
- var j = other. characters . count
25
+ var i = self . count
26
+ var j = other. count
27
27
var charInSequence = self . endIndex
28
28
29
29
var lcs = String ( )
@@ -41,7 +41,7 @@ extension String {
41
41
lcs. append ( self [ charInSequence] )
42
42
}
43
43
}
44
- return String ( lcs. characters . reversed ( ) )
44
+ return String ( lcs. reversed ( ) )
45
45
}
46
46
47
47
return backtrack ( lcsLength ( other) )
0 commit comments