Skip to content

Commit d15c1b8

Browse files
committed
Fix method name from reverse() to reversed() in README.markdown
1 parent bad00fe commit d15c1b8

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Longest Common Subsequence/README.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ func backtrack(_ matrix: [[Int]]) -> String {
152152
}
153153
}
154154

155-
return String(lcs.characters.reverse())
155+
return String(lcs.characters.reversed())
156156
}
157157
```
158158

159159
This backtracks from `matrix[n+1][m+1]` (bottom-right corner) to `matrix[1][1]` (top-right corner), looking for characters that are common to both strings. It adds those characters to a new string, `lcs`.
160160

161161
The `charInSequence` variable is an index into the string given by `self`. Initially this points to the last character of the string. Each time we decrement `i`, we also move back `charInSequence`. When the two characters are found to be equal, we add the character at `self[charInSequence]` to the new `lcs` string. (We can't just write `self[i]` because `i` may not map to the current position inside the Swift string.)
162162

163-
Due to backtracking, characters are added in reverse order, so at the end of the function we call `reverse()` to put the string in the right order. (Appending new characters to the end of the string and then reversing it once is faster than always inserting the characters at the front of the string.)
163+
Due to backtracking, characters are added in reverse order, so at the end of the function we call `reversed()` to put the string in the right order. (Appending new characters to the end of the string and then reversing it once is faster than always inserting the characters at the front of the string.)
164164

165165
## Putting it all together
166166

0 commit comments

Comments
 (0)