Skip to content

Commit 1b855ff

Browse files
authored
Update longest_common_subsequence.py
1 parent f19b7e8 commit 1b855ff

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Dynamic Programming/Longest Common Subsequence/longest_common_subsequence.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66

77
def lcs(n, m, s1, s2):
8-
if n == 0 or m == 0:
8+
if n == 0 or m == 0: # if the one of the string size 0 then we returned 0
99
return 0
10-
if s1[n - 1] == s2[m - 1]:
10+
if s1[n - 1] == s2[m - 1]: # if string have the same letter then we add one and go to the next letter
1111
return 1 + lcs(n - 1, m - 1, s1, s2);
12-
else:
12+
else: # otherwise we do maximum of both strings
1313
return max(lcs(n, m - 1, s1, s2), lcs(n - 1, m, s1, s2))
1414

1515
if __name__ == "__main__":
1616
print("Longest Common Subsequence Problem")
17-
s1 = input("First String: ")
18-
s2 = input("Second String: ")
19-
print("Length of the Longest Common Subsequence: ", lcs(len(s1), len(s2), s1, s2))
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)