-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathlongestCommonSubsequence.py
87 lines (61 loc) · 3.06 KB
/
longestCommonSubsequence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# https://leetcode.com/problems/longest-common-subsequence/
class Solution(object):
# TLE
# Plain Recursion
def longestCommonSubsequence(self, text1, text2):
"""
:type text1: str
:type text2: str
:rtype: int
"""
def helper(text1,text2, pointerText1, pointerText2):
if (pointerText1<0 or pointerText2<0):
return 0
if text1[pointerText1]==text2[pointerText2]:
return 1+ helper(text1, text2, pointerText1-1, pointerText2-1)
else :
return max(helper(text1,text2,pointerText1-1,pointerText2), helper(text1, text2, pointerText1, pointerText2-1))
return helper(text1, text2, len(text1)-1, len(text2)-1)
# Memoized Version of Above
# Runtime: 930 ms, faster than 19.39% of Python online submissions for Longest Common Subsequence.
# Memory Usage: 23.4 MB, less than 16.88% of Python online submissions for Longest Common Subsequence.
def longestCommonSubsequence(self, text1, text2):
"""
:type text1: str
:type text2: str
:rtype: int
"""
dp = [[-1] * (len(text2)+1) for i in range(len(text1)+1)]
def helper(text1, text2, pointerText1, pointerText2):
if (pointerText1 < 0 or pointerText2 < 0):
return 0
if dp[pointerText1][pointerText2] != -1:
return dp[pointerText1][pointerText2]
res = 0
if text1[pointerText1] == text2[pointerText2]:
res = 1 + helper(text1, text2, pointerText1-1, pointerText2-1)
else:
res = max(helper(text1, text2, pointerText1-1, pointerText2),
helper(text1, text2, pointerText1, pointerText2-1))
dp[pointerText1][pointerText2] = res
return res
return helper(text1, text2, len(text1)-1, len(text2)-1)
# Runtime: 406 ms, faster than 51.49% of Python online submissions for Longest Common Subsequence.
# Memory Usage: 21.2 MB, less than 91.06% of Python online submissions for Longest Common Subsequence.
def longestCommonSubsequence(self, text1, text2):
"""
:type text1: str
:type text2: str
:rtype: int
"""
dp = [[0] * (len(text1)+1) for i in range (len(text2)+1)]
for pointerText2 in range (1,len(text2)+1):
for pointerText1 in range (1,len(text1)+1):
if text1[pointerText1-1] == text2[pointerText2-1]:
dp[pointerText2][pointerText1] = 1 + dp[pointerText2-1][pointerText1-1]
else:
dp[pointerText2][pointerText1] = max(dp[pointerText2-1][pointerText1], dp[pointerText2][pointerText1-1])
return dp[-1][-1]
print(Solution().longestCommonSubsequence("abcde", "ace")) #3
print(Solution().longestCommonSubsequence("abc", "def")) #0
print(Solution().longestCommonSubsequence("sfd", "sfdasdgsdf")) #3