Skip to content

Commit 3851362

Browse files
Merge pull request #203 from manthan0227/main
Added Longest Common Subsequence
2 parents 9865e25 + 1b855ff commit 3851362

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
20.6 KB
Loading
23.3 KB
Loading
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
# Longest Common Subsequence
3+
### Aim
4+
- The aim of the script is to find out the maximum length of the common sequence between two strings.
5+
6+
### Purpose
7+
- The main purpose of this script is to show the implementaion of recursion to solve the Longest Common Subsequence.
8+
9+
### Description
10+
Given two string and determine the length of the string which is common subsequence.
11+
12+
### Workflow
13+
`Input:`
14+
s1 = "ABCDEFG",
15+
s2 = "DEFGHI"
16+
17+
`output:`
18+
Length of the LCS: 4
19+
20+
### Explanation
21+
- Find the number of subsequences with lengths ranging from 1,2,..n-1. In Recursion we first passed the two string and length of the two string as parameters.
22+
- We first find the size of the both string if they are both empty string then we return 0.
23+
- And if we matching the charcter of two string and those two charcters are matched then we add one.
24+
- Otherwise we check, if next charcter is match to the this charcter than we go to that side and decrease the size of the string and add one to our answer.
25+
- At the last we get the length of the longest common subsequence.
26+
27+
### Time and Space Complexity
28+
- Time Complexity: `O(len_of_string1*len_of_string2)`
29+
- Space Complexity:`O(len_of_the_lcs)`
30+
31+
## 💻 Input and Output
32+
- **Test Case 1 :**
33+
34+
![](https://github.com/manthan0227/PyAlgo-Tree/blob/main/Dynamic%20Programming/Longest%20Common%20Subsequence/Images/TEST_1.png)
35+
36+
- **Test Case 2 :**
37+
38+
![](https://github.com/manthan0227/PyAlgo-Tree/blob/main/Dynamic%20Programming/Longest%20Common%20Subsequence/Images/TEST_2.png)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# LCS: There are two strings in which we have to find the length of the longest common subsequence
2+
# Subarray: Continuous sequence in an array
3+
# Subsequence: Need not to be contiguous, but maintains order
4+
# Subset: Same as subsequence except it has empty set
5+
6+
7+
def lcs(n, m, s1, s2):
8+
if n == 0 or m == 0: # if the one of the string size 0 then we returned 0
9+
return 0
10+
if s1[n - 1] == s2[m - 1]: # if string have the same letter then we add one and go to the next letter
11+
return 1 + lcs(n - 1, m - 1, s1, s2);
12+
else: # otherwise we do maximum of both strings
13+
return max(lcs(n, m - 1, s1, s2), lcs(n - 1, m, s1, s2))
14+
15+
if __name__ == "__main__":
16+
print("Longest Common Subsequence Problem")
17+
s1 = input("First String: ") # input the first string
18+
s2 = input("Second String: ") # input the second string
19+
print("Length of the Longest Common Subsequence: ", lcs(len(s1), len(s2), s1, s2)) # len(s) is length of the string s

0 commit comments

Comments
 (0)