File tree

1 file changed

+5
-8
lines changed
  • pygorithm/dynamic_programming

1 file changed

+5
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,16 @@ def longest_common_subsequence(s1, s2):
2828
:param s2: string
2929
:return: int
3030
"""
31-
m = len(s1)
32-
n = len(s2)
31+
m, n = len(s1), len(s2)
3332

34-
dp = [[0] * (n + 1) for i in range(m + 1)]
33+
dp = [[0] * (n + 1)] * (m + 1)
3534
"""
3635
dp[i][j] : contains length of LCS of s1[0..i-1] and s2[0..j-1]
3736
"""
3837

39-
for i in range(m + 1):
40-
for j in range(n + 1):
41-
if i == 0 or j == 0:
42-
dp[i][j] = 0
43-
elif s1[i - 1] == s2[j - 1]:
38+
for i in range(1, m + 1):
39+
for j in range(1, n + 1):
40+
if s1[i - 1] == s2[j - 1]:
4441
dp[i][j] = dp[i - 1][j - 1] + 1
4542
else:
4643
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

0 commit comments

Comments
 (0)