Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b585a9e

Browse files
committed
day6:LCS in python
1 parent ed1f769 commit b585a9e

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

‎README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
| Current Status| Stats |
88
| :------------: | :----------: |
99
| Total C++ Problems | 188 |
10-
| Total Python Problems | 10 |
11-
| Current Daily Streak| 5 |
10+
| Total Python Problems | 11 |
11+
| Current Daily Streak| 6 |
1212
| Last Streak | 06/20/2019 - 06/21/2019|
13-
| Current Streak | 06/23/2019 - 06/27/2019|
13+
| Current Streak | 06/23/2019 - 06/28/2019|
1414

1515
</center>
1616

@@ -106,7 +106,7 @@ Include contains single header implementation of data structures and some algori
106106
| Problem | Solution |
107107
| :------------ | :----------: |
108108
| N<sup>th</sup> Fibonacci term using different memoization techniques | [fibonacci.cpp](dynamic_programming_problems/fibonacci.cpp)|
109-
| Longest Common Subsequence Problem | [lcs.cpp](dynamic_programming_problems/lcs.cpp) |
109+
| Longest Common Subsequence Problem | [lcs.cpp](dynamic_programming_problems/lcs.cpp), [longest_common_subsequence.py](dynamic_programming_problems/longest_common_subsequence.py) |
110110
| Maximum Value Contigous Subsequence Problem [wiki](https://en.wikipedia.org/wiki/Maximum_subarray_problem)| [max_subsequence.cpp](dynamic_programming_problems/max_subsequence.cpp)|
111111
| Catalan number - Count the number of possible Binary Search Trees with n keys | [catalan_number.cpp](dynamic_programming_problems/catalan_number.cpp)|
112112
| Calculate the number of unique paths from source origin (0, 0) to destination (m-1, n-1) in a m x n grid. You can only move either in down or right direction.|[unique_paths.cpp](dynamic_programming_problems/unique_paths.cpp)|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Longest common Subsequence problem
3+
If we are given two sequences, find the length of longest common subsequences present in both of them.
4+
For example:
5+
String1: helloworld
6+
String2: ellorald
7+
Longest common substring should be "ellorld" of size 7
8+
"""
9+
10+
def lcs(str1, str2):
11+
"""Function to calculate longest common substring between str1 and str2"""
12+
m = len(str1)
13+
n = len(str2)
14+
15+
dp = [[None] * (n+1) for i in range(m+1)]
16+
17+
for i in range(m+1):
18+
for j in range(n+1):
19+
if i == 0 or j == 0:
20+
dp[i][j] = 0
21+
elif str1[i-1] == str2[j-1]:
22+
dp[i][j] = 1 + dp[i-1][j-1]
23+
else:
24+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
25+
26+
return dp[m][n]
27+
28+
29+
if __name__ == "__main__":
30+
str1 = "helloworld"
31+
str2 = "ellorald"
32+
33+
print(F"The length of longest common subsequence between '{str1}' and '{str2}' is:", lcs(str1, str2))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /