Skip to content

Commit 1937d70

Browse files
committed
weeok 08 solution longest common seq
1 parent d750252 commit 1937d70

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
3+
# Dynamic programming
4+
n = len(text1)
5+
m = len(text2)
6+
7+
# text1 - col, text2 - row
8+
dp= [[0] * (n+1) for _ in range(m+1)]
9+
10+
for i in range(1, m+1):
11+
for j in range(1, n+1):
12+
if text2[i-1] == text1[j-1]:
13+
dp[i][j] = dp[i-1][j-1] + 1
14+
else:
15+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
16+
17+
return dp[m][n]

0 commit comments

Comments
 (0)