Skip to content
Merged
Changes from 2 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
74 changes: 74 additions & 0 deletions dynamic_programming/longest_common_substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
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", "xabded" have the length of longest common substring 2 ("ab" or "de").
"""


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

Parameters
----------
t1: str, one of the strings
t2: str, the other string

Returns
-------
ans: int, the length of the longest common substring.

>>> 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
"""

assert t1 is not None
assert t2 is not None

t1_length = len(t1)
t2_length = len(t2)

dp = [[0] * (t2_length + 1) for _ in range(t1_length + 1)]
ans = 0

for i in range(1, t1_length + 1):
for j in range(1, t2_length + 1):
if t1[i - 1] == t2[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()