Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit fa1f2c4

Browse files
committed
finished solution for problem64
1 parent 85aa9cf commit fa1f2c4

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

problem64/Solution.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

problem64/Solution0.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
3+
n, m = len(text1), len(text2)
4+
cache = [[0] * m for _ in range(n)]
5+
has_cache = [[False] * m for _ in range(n)]
6+
7+
def findLCS(i, j):
8+
if i == n or j == m: return 0
9+
if has_cache[i][j]: return cache[i][j]
10+
11+
if text1[i] != text2[j]:
12+
cache[i][j] = max(
13+
findLCS(i+1, j),
14+
findLCS(i, j+1)
15+
)
16+
else: cache[i][j] = findLCS(i+1, j+1) +1
17+
18+
has_cache[i][j] = True
19+
return cache[i][j]
20+
21+
return findLCS(0,0)

0 commit comments

Comments
 (0)