Skip to content

Commit daa1641

Browse files
committed
feat: [Week 08-4] solve common longest subsequence
1 parent a4b1982 commit daa1641

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Solution: ์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•œ ํ’€์ด, memo ๋ฅผ ํ™œ์šฉํ•œ ์‹œ๊ฐ„๋ณต์žก๋„ ๊ฐœ์„ 
3+
4+
Time: O(m * n)
5+
Space: O(2 * m * n)
6+
"""
7+
8+
9+
class Solution:
10+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
11+
memo = {}
12+
13+
def dfs(i, j):
14+
if (i, j) in memo:
15+
return memo[(i, j)]
16+
17+
if i == len(text1) or j == len(text2):
18+
memo[(i, j)] = 0
19+
elif text1[i] == text2[j]:
20+
memo[(i, j)] = 1 + dfs(i + 1, j + 1)
21+
else:
22+
memo[(i, j)] = max(dfs(i + 1, j), dfs(i, j + 1))
23+
return memo[(i, j)]
24+
25+
return dfs(0, 0)

0 commit comments

Comments
ย (0)