We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0be2539 commit dffa590Copy full SHA for dffa590
pygorithm/dynamic_programming/lcs.py
@@ -28,19 +28,16 @@ def longest_common_subsequence(s1, s2):
28
:param s2: string
29
:return: int
30
"""
31
- m = len(s1)
32
- n = len(s2)
+ m, n = len(s1), len(s2)
33
34
- dp = [[0] * (n + 1) for i in range(m + 1)]
+ dp = [[0] * (n + 1)] * (m + 1)
35
36
dp[i][j] : contains length of LCS of s1[0..i-1] and s2[0..j-1]
37
38
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]:
+ for i in range(1, m + 1):
+ for j in range(1, n + 1):
+ if s1[i - 1] == s2[j - 1]:
44
dp[i][j] = dp[i - 1][j - 1] + 1
45
else:
46
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
0 commit comments