From c31ef5e7782803b07e6d7eb4dca3b038cbdb095d Mon Sep 17 00:00:00 2001 From: RohitSingh107 <64142943+RohitSingh107@users.noreply.github.com> Date: Wed, 26 Oct 2022 03:25:48 +0530 Subject: [PATCH] Add longest common substring (#7488) * added longest common substring * added retrun type hint * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * changed t1, t2 to text1, text2 * Update longest_common_substring.py * Update dynamic_programming/longest_common_substring.py Co-authored-by: Chris O <46587501+ChrisO345@users.noreply.github.com> * Update dynamic_programming/longest_common_substring.py Co-authored-by: Chris O <46587501+ChrisO345@users.noreply.github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * applied suggested changes * Update dynamic_programming/longest_common_substring.py Co-authored-by: Caeden Perelli-Harris * removed space between line * return longest common substring * Update dynamic_programming/longest_common_substring.py Co-authored-by: Christian Clauss * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Caeden Perelli-Harris Co-authored-by: Chris O <46587501+ChrisO345@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss --- .../longest_common_substring.py | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 dynamic_programming/longest_common_substring.py diff --git a/dynamic_programming/longest_common_substring.py b/dynamic_programming/longest_common_substring.py new file mode 100644 index 000000000..84a9f1860 --- /dev/null +++ b/dynamic_programming/longest_common_substring.py @@ -0,0 +1,63 @@ +""" +Longest Common Substring Problem Statement: Given two sequences, find the +longest common substring present in both of them. A substring is +necessarily continuous. +Example: "abcdef" and "xabded" have two longest common substrings, "ab" or "de". +Therefore, algorithm should return any one of them. +""" + + +def longest_common_substring(text1: str, text2: str) -> str: + """ + Finds the longest common substring between two strings. + >>> longest_common_substring("", "") + '' + >>> longest_common_substring("a","") + '' + >>> longest_common_substring("", "a") + '' + >>> longest_common_substring("a", "a") + 'a' + >>> longest_common_substring("abcdef", "bcd") + 'bcd' + >>> longest_common_substring("abcdef", "xabded") + 'ab' + >>> longest_common_substring("GeeksforGeeks", "GeeksQuiz") + 'Geeks' + >>> longest_common_substring("abcdxyz", "xyzabcd") + 'abcd' + >>> longest_common_substring("zxabcdezy", "yzabcdezx") + 'abcdez' + >>> longest_common_substring("OldSite:GeeksforGeeks.org", "NewSite:GeeksQuiz.com") + 'Site:Geeks' + >>> longest_common_substring(1, 1) + Traceback (most recent call last): + ... + ValueError: longest_common_substring() takes two strings for inputs + """ + + 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) + + dp = [[0] * (text2_length + 1) for _ in range(text1_length + 1)] + ans_index = 0 + ans_length = 0 + + 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] = 1 + dp[i - 1][j - 1] + if dp[i][j] > ans_length: + ans_index = i + ans_length = dp[i][j] + + return text1[ans_index - ans_length : ans_index] + + +if __name__ == "__main__": + import doctest + + doctest.testmod()