Skip to content

Commit ed509f8

Browse files
authored
Update subsequence_algorithms.py
1 parent 5f47908 commit ed509f8

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

dynamic_programming/subsequence_algorithms.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def longest_common_subsequence(first_sequence: str, second_sequence: str):
8484
----------
8585
first_sequence: str
8686
The first sequence (or string).
87-
87+
8888
second_sequence: str
8989
The second sequence (or string).
9090
@@ -107,12 +107,12 @@ def longest_common_subsequence(first_sequence: str, second_sequence: str):
107107
>>> longest_common_subsequence("abc", "abc")
108108
(3, 'abc')
109109
"""
110-
m, n = len(x), len(y)
110+
m, n = len(first_sequence), len(second_sequence)
111111
dp = [[0] * (n + 1) for _ in range(m + 1)]
112112

113113
for i in range(1, m + 1):
114114
for j in range(1, n + 1):
115-
if x[i - 1] == y[j - 1]:
115+
if first_sequence[i - 1] == second_sequence[j - 1]:
116116
dp[i][j] = dp[i - 1][j - 1] + 1
117117
else:
118118
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
@@ -121,8 +121,8 @@ def longest_common_subsequence(first_sequence: str, second_sequence: str):
121121
i, j = m, n
122122
lcs = []
123123
while i > 0 and j > 0:
124-
if x[i - 1] == y[j - 1]:
125-
lcs.append(x[i - 1])
124+
if first_sequence[i - 1] == second_sequence[j - 1]:
125+
lcs.append(first_sequence[i - 1])
126126
i -= 1
127127
j -= 1
128128
elif dp[i - 1][j] > dp[i][j - 1]:
@@ -133,6 +133,7 @@ def longest_common_subsequence(first_sequence: str, second_sequence: str):
133133
return dp[m][n], "".join(reversed(lcs))
134134

135135

136+
136137
if __name__ == "__main__":
137138
import doctest
138139

0 commit comments

Comments
 (0)