Skip to content

Commit cf0fc60

Browse files
committed
solve : longest common subsequence
1 parent 99ec887 commit cf0fc60

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# m is the length of text1 and n is the length of text2
2+
# TC : O(m * n)
3+
# SC : O(m * n)
4+
class Solution:
5+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
6+
dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)]
7+
for r in range(1, len(text1) + 1):
8+
for c in range(1, len(text2) + 1):
9+
if text1[r - 1] == text2[c - 1]:
10+
dp[r][c] = 1 + dp[r - 1][c - 1]
11+
else:
12+
dp[r][c] = max(dp[r - 1][c], dp[r][c - 1])
13+
return dp[-1][-1]

0 commit comments

Comments
 (0)