Skip to content
Merged
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4abd7bf
added longest common substring
RohitSingh107 Oct 21, 2022
9437f2d
added retrun type hint
RohitSingh107 Oct 21, 2022
4f4e53f
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
d5cc1e1
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
7166951
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
7c2f8a0
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
eaf0fc6
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
2096aec
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
b9dc140
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
59661ea
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
a2b4d89
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
eb4730a
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
085422e
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
5094fe9
changed t1, t2 to text1, text2
RohitSingh107 Oct 22, 2022
cd6c12f
Update longest_common_substring.py
RohitSingh107 Oct 22, 2022
3682cdb
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
0d00e8f
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 22, 2022
ca0e139
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 22, 2022
ada8be8
applied suggested changes
RohitSingh107 Oct 22, 2022
46bbaa4
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 23, 2022
2760b2f
removed space between line
RohitSingh107 Oct 23, 2022
97f1f80
return longest common substring
RohitSingh107 Oct 23, 2022
0d94dfe
Update dynamic_programming/longest_common_substring.py
RohitSingh107 Oct 23, 2022
e8365e4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2022
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions dynamic_programming/longest_common_substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Longest Common Substring Problem Statement: Given two sequences, find the length
of 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, the length is 2.
"""


def longest_common_substring(text1: str, text2: str) -> int:
"""
Finds the length of longest common substring between two strings.


>>> longest_common_substring("", "")
0
>>> longest_common_substring("a","")
0
>>> longest_common_substring("", "a")
0
>>> longest_common_substring("a", "a")
1
>>> longest_common_substring("abcdef", "bcd")
3
>>> longest_common_substring("abcdef", "xabded")
2
>>> longest_common_substring("GeeksforGeeks", "GeeksQuiz")
5
>>> longest_common_substring("abcdxyz", "xyzabcd")
4
>>> longest_common_substring("zxabcdezy", "yzabcdezx")
6
>>> longest_common_substring("OldSite:GeeksforGeeks.org", "NewSite:GeeksQuiz.com")
10
>>> 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 = 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]
ans = max(ans, dp[i][j])

return ans


if __name__ == "__main__":
import doctest

doctest.testmod()