From 31b9d64ae92db6fbc916df21476af17b41c68194 Mon Sep 17 00:00:00 2001 From: jaimin45-art Date: Thu, 16 Oct 2025 18:06:37 +0530 Subject: [PATCH 1/3] Added Python solution for Longest Common Subsequence problem --- strings/longest_common_subsequence.py | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 strings/longest_common_subsequence.py diff --git a/strings/longest_common_subsequence.py b/strings/longest_common_subsequence.py new file mode 100644 index 000000000000..9ec675333f33 --- /dev/null +++ b/strings/longest_common_subsequence.py @@ -0,0 +1,47 @@ +# longest_common_subsequence.py +# This program finds the Longest Common Subsequence (LCS) between two strings +# using Dynamic Programming and also prints the actual LCS string. + +def longest_common_subsequence(X: str, Y: str): + m, n = len(X), len(Y) + dp = [[0] * (n + 1) for _ in range(m + 1)] + + # Fill the dp table + for i in range(1, m + 1): + for j in range(1, n + 1): + if X[i - 1] == Y[j - 1]: + dp[i][j] = 1 + dp[i - 1][j - 1] + else: + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + + # Reconstruct the LCS string + i, j = m, n + lcs = [] + while i > 0 and j > 0: + if X[i - 1] == Y[j - 1]: + lcs.append(X[i - 1]) + i -= 1 + j -= 1 + elif dp[i - 1][j] > dp[i][j - 1]: + i -= 1 + else: + j -= 1 + + lcs.reverse() + lcs_str = ''.join(lcs) + + # Print DP table (for visualization) + print("DP Table:") + for row in dp: + print(row) + + print(f"\nLength of LCS: {dp[m][n]}") + print(f"LCS String: {lcs_str}") + + return dp[m][n], lcs_str + + +if __name__ == "__main__": + s1 = input("Enter first string: ") + s2 = input("Enter second string: ") + longest_common_subsequence(s1, s2) From 9fa8a3fffee607e613d54f0dad485aeeccff01f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Oct 2025 12:42:02 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/longest_common_subsequence.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/strings/longest_common_subsequence.py b/strings/longest_common_subsequence.py index 9ec675333f33..6731e6c09e9d 100644 --- a/strings/longest_common_subsequence.py +++ b/strings/longest_common_subsequence.py @@ -2,6 +2,7 @@ # This program finds the Longest Common Subsequence (LCS) between two strings # using Dynamic Programming and also prints the actual LCS string. + def longest_common_subsequence(X: str, Y: str): m, n = len(X), len(Y) dp = [[0] * (n + 1) for _ in range(m + 1)] @@ -28,7 +29,7 @@ def longest_common_subsequence(X: str, Y: str): j -= 1 lcs.reverse() - lcs_str = ''.join(lcs) + lcs_str = "".join(lcs) # Print DP table (for visualization) print("DP Table:") From 9b5b565b20ff58c951a3cd93e79ffa47494bb06e Mon Sep 17 00:00:00 2001 From: jaimin45-art Date: Thu, 16 Oct 2025 18:48:57 +0530 Subject: [PATCH 3/3] fix: changed variable names to lowercase as per ruff style guide --- strings/longest_common_subsequence.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/strings/longest_common_subsequence.py b/strings/longest_common_subsequence.py index 6731e6c09e9d..7f4569fdf33a 100644 --- a/strings/longest_common_subsequence.py +++ b/strings/longest_common_subsequence.py @@ -3,14 +3,14 @@ # using Dynamic Programming and also prints the actual LCS string. -def longest_common_subsequence(X: str, Y: str): - m, n = len(X), len(Y) +def longest_common_subsequence(x: str, y: str): + m, n = len(x), len(y) dp = [[0] * (n + 1) for _ in range(m + 1)] # Fill the dp table for i in range(1, m + 1): for j in range(1, n + 1): - if X[i - 1] == Y[j - 1]: + if x[i - 1] == y[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) @@ -19,8 +19,8 @@ def longest_common_subsequence(X: str, Y: str): i, j = m, n lcs = [] while i > 0 and j > 0: - if X[i - 1] == Y[j - 1]: - lcs.append(X[i - 1]) + if x[i - 1] == y[j - 1]: + lcs.append(x[i - 1]) i -= 1 j -= 1 elif dp[i - 1][j] > dp[i][j - 1]: