Skip to content

Commit

Permalink
Update 01. Longest common subsequence Recursive.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Mar 2, 2023
1 parent 45a78cd commit 222baee
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,24 @@ def LCS(X, Y, n, m):
s = Solution()
print s.longestCommonSubsequence("ylqpejqbalahwr", "yrkzavgdmdgtqpg")
'''
'''


class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m = len(text1)
n = len(text2)
dp = [[0]*(n+1) for i in range(m+1)]

for i in range(1, m+1):
for j in range(1, n+1):
if text1[i-1] == text2[j-1]:
dp[i][j] = 1 + dp[i-1][j-1]
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])

return dp[-1][-1]


# Time: O(n * m)
# Space: O(n * m)

0 comments on commit 222baee

Please sign in to comment.