From a19259d658cb7fe0d054b24d21f604888879e0b2 Mon Sep 17 00:00:00 2001 From: Alfredo Hernandez Baeza Date: Fri, 2 May 2025 10:51:03 -0400 Subject: [PATCH 1/4] Update longest_common_substring.py - Combined the ans_index and ans_length into a single tuple to track the best match (position + length) more cleanly. - Early exit for empty strings. --- .../longest_common_substring.py | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/dynamic_programming/longest_common_substring.py b/dynamic_programming/longest_common_substring.py index ea5233eb2d17..62dd9d392edb 100644 --- a/dynamic_programming/longest_common_substring.py +++ b/dynamic_programming/longest_common_substring.py @@ -43,22 +43,21 @@ def longest_common_substring(text1: str, text2: str) -> str: if not (isinstance(text1, str) and isinstance(text2, str)): raise ValueError("longest_common_substring() takes two strings for inputs") - text1_length = len(text1) - text2_length = len(text2) + if not text1 or not text2: + return '' - dp = [[0] * (text2_length + 1) for _ in range(text1_length + 1)] - ans_index = 0 - ans_length = 0 + dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)] + max_length, end_pos = 0, 0 - for i in range(1, text1_length + 1): - for j in range(1, text2_length + 1): + for i in range(1, len(text1) + 1): + for j in range(1, len(text2) + 1): if text1[i - 1] == text2[j - 1]: - dp[i][j] = 1 + dp[i - 1][j - 1] - if dp[i][j] > ans_length: - ans_index = i - ans_length = dp[i][j] + dp[i][j] = dp[i - 1][j - 1] + 1 + if dp[i][j] > max_length: + max_length = dp[i][j] + end_pos = i - return text1[ans_index - ans_length : ans_index] + return text1[end_pos - max_length:end_pos] if __name__ == "__main__": From 53b1b10f4503d3ef1d8587d777da491d0c3090b6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 2 May 2025 14:51:47 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_common_substring.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/longest_common_substring.py b/dynamic_programming/longest_common_substring.py index 62dd9d392edb..d526d8a275f4 100644 --- a/dynamic_programming/longest_common_substring.py +++ b/dynamic_programming/longest_common_substring.py @@ -44,7 +44,7 @@ def longest_common_substring(text1: str, text2: str) -> str: raise ValueError("longest_common_substring() takes two strings for inputs") if not text1 or not text2: - return '' + return "" dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)] max_length, end_pos = 0, 0 @@ -57,7 +57,7 @@ def longest_common_substring(text1: str, text2: str) -> str: max_length = dp[i][j] end_pos = i - return text1[end_pos - max_length:end_pos] + return text1[end_pos - max_length : end_pos] if __name__ == "__main__": From d8dfc28a50e7f02fda5a858f7ab31795cf6ee663 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 10 May 2025 14:55:14 +0300 Subject: [PATCH 3/4] Update longest_common_substring.py --- dynamic_programming/longest_common_substring.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/longest_common_substring.py b/dynamic_programming/longest_common_substring.py index d526d8a275f4..5e808de412d9 100644 --- a/dynamic_programming/longest_common_substring.py +++ b/dynamic_programming/longest_common_substring.py @@ -46,16 +46,20 @@ def longest_common_substring(text1: str, text2: str) -> str: if not text1 or not text2: return "" - dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)] - max_length, end_pos = 0, 0 + text1_length = len(text1) + text2_length = len(text2) + + dp = [[0] * (text2_length + 1) for _ in range(text1_length + 1)] + end_pos = 0 + max_length = 0 - for i in range(1, len(text1) + 1): - for j in range(1, len(text2) + 1): + for i in range(1, text1_length + 1): + for j in range(1, text2_length + 1): if text1[i - 1] == text2[j - 1]: - dp[i][j] = dp[i - 1][j - 1] + 1 + dp[i][j] = 1 + dp[i - 1][j - 1] if dp[i][j] > max_length: - max_length = dp[i][j] end_pos = i + max_length = dp[i][j] return text1[end_pos - max_length : end_pos] From f7549a1d58277e0ff82ce4a075c0440fb290e688 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 May 2025 11:55:37 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_common_substring.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/longest_common_substring.py b/dynamic_programming/longest_common_substring.py index 5e808de412d9..3ba83f3d9f03 100644 --- a/dynamic_programming/longest_common_substring.py +++ b/dynamic_programming/longest_common_substring.py @@ -48,7 +48,7 @@ def longest_common_substring(text1: str, text2: str) -> str: text1_length = len(text1) text2_length = len(text2) - + dp = [[0] * (text2_length + 1) for _ in range(text1_length + 1)] end_pos = 0 max_length = 0