Skip to content

Add LongestCommonSubstring Impleamentation#7464

Open
Rosander0 wants to merge 11 commits into
TheAlgorithms:masterfrom
Rosander0:add-longest-common-substring
Open

Add LongestCommonSubstring Impleamentation#7464
Rosander0 wants to merge 11 commits into
TheAlgorithms:masterfrom
Rosander0:add-longest-common-substring

Conversation

@Rosander0

Copy link
Copy Markdown
  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.82%. Comparing base (6fbbc94) to head (64bfecb).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7464      +/-   ##
============================================
+ Coverage     79.80%   79.82%   +0.02%     
- Complexity     7302     7314      +12     
============================================
  Files           803      804       +1     
  Lines         23751    23765      +14     
  Branches       4671     4675       +4     
============================================
+ Hits          18955    18971      +16     
  Misses         4036     4036              
+ Partials        760      758       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rosander0 Rosander0 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This contians the URL link to documetation explaining the longest common substring algorithm
https://www.geeksforgeeks.org/dsa/longest-common-substring-dp-29/

*/
public static String longestCommonSubstring(final String a, final String b) {
if (a == null || b == null || a.isEmpty() || b.isEmpty()) {
return "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add reference URL in Javadoc as per contributing guidelines:
@see Wikipedia

}
}
}
return a.substring(endIndex - maxLength, endIndex);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When maxLength == 0, this returns "" by coincidence.
Please add explicit check:

if (maxLength == 0) {
return "";
}
return a.substring(endIndex - maxLength, endIndex);

@Test
public void testMultipleMatchesFirstLongest() {
// Keeps the first matched longest substring when lengths are tied
assertEquals("abc", LongestCommonSubstring.longestCommonSubstring("abcXdef", "abcYdef"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tie-breaking behavior should also be documented in the
method's Javadoc in implementation file, not just in test comment.

@prashantpiyush1111 prashantpiyush1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Rosander0, good work on the implementation! The DP approach is correct
and test coverage is solid. Just a few things to fix before this can be merged:

1. Reference URL missing from source file
Contributing guidelines require a Wikipedia/reference link directly in the
Javadoc of the implementation file, not just in the PR comments.

@see Wikipedia: Longest Common Substring

2. Fragile edge case
When no common substring exists, the method returns "" by coincidence (substring(0,0)).
Please add an explicit guard:

if (maxLength == 0) {
return "";
}

3. Tie-breaking behavior undocumented
The "first longest wins" contract should be documented in the method's
Javadoc in the implementation file, not just in the test comment.

4. Branch needs cleanup
There are 11 commits with duplicates and unnecessary merge commits:

  • "fix: resolve build errors and formatting" appears twice
  • "fix: add blank line before imports" appears twice
  • 2 unnecessary merge commits

Please squash into 1 clean commit:

git rebase -i HEAD~11

Final commit message should be:
"feat: add LongestCommonSubstring implementation"

5. Minor
Typo in PR title: "Impleamentation" → "Implementation"


Overall the logic is clean, all CI checks are passing and test coverage
is 100%. Fix these small things and this should be good to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants