-
- Notifications
You must be signed in to change notification settings - Fork 49.2k
Add longest common substring #7488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
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 9437f2d added retrun type hint
RohitSingh107 4f4e53f Update dynamic_programming/longest_common_substring.py
RohitSingh107 d5cc1e1 Update dynamic_programming/longest_common_substring.py
RohitSingh107 7166951 Update dynamic_programming/longest_common_substring.py
RohitSingh107 7c2f8a0 Update dynamic_programming/longest_common_substring.py
RohitSingh107 eaf0fc6 Update dynamic_programming/longest_common_substring.py
RohitSingh107 2096aec Update dynamic_programming/longest_common_substring.py
RohitSingh107 b9dc140 Update dynamic_programming/longest_common_substring.py
RohitSingh107 59661ea Update dynamic_programming/longest_common_substring.py
RohitSingh107 a2b4d89 Update dynamic_programming/longest_common_substring.py
RohitSingh107 eb4730a Update dynamic_programming/longest_common_substring.py
RohitSingh107 085422e Update dynamic_programming/longest_common_substring.py
RohitSingh107 5094fe9 changed t1, t2 to text1, text2
RohitSingh107 cd6c12f Update longest_common_substring.py
RohitSingh107 3682cdb Update dynamic_programming/longest_common_substring.py
RohitSingh107 0d00e8f Update dynamic_programming/longest_common_substring.py
RohitSingh107 ca0e139 [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ada8be8 applied suggested changes
RohitSingh107 46bbaa4 Update dynamic_programming/longest_common_substring.py
RohitSingh107 2760b2f removed space between line
RohitSingh107 97f1f80 return longest common substring
RohitSingh107 0d94dfe Update dynamic_programming/longest_common_substring.py
RohitSingh107 e8365e4 [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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: | ||
RohitSingh107 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| """ | ||
| Finds the length of longest common substring between two strings. | ||
| | ||
| | ||
RohitSingh107 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| >>> 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 | ||
| """ | ||
RohitSingh107 marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| 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() | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.