Skip to content

Conversation

@codomposer
Copy link
Contributor

@codomposer codomposer commented Nov 5, 2025

Add Manacher's Algorithm for Longest Palindromic Substring

Summary

This PR implements Manacher's Algorithm, a linear-time O(n) algorithm for finding the longest palindromic substring in a string. This is a significant improvement over the naive O(n²) approach.

What's Added

  • Algorithm Implementation (Algorithms/Strings/ManachersAlgorithm.cs):

    • FindLongestPalindrome() - Returns the longest palindromic substring
    • FindLongestPalindromeWithDetails() - Returns palindrome with position details
    • IsPalindrome() - Checks if entire string is a palindrome
    • Comprehensive inline documentation explaining the algorithm's logic
  • Comprehensive Test Suite (Algorithms.Tests/Strings/ManachersAlgorithmTests.cs):

    • 28 test cases covering:
      • Odd and even-length palindromes
      • Edge cases (empty, single character, no palindromes)
      • Special characters and numeric strings
      • Performance validation with long strings
  • Documentation:

    • Updated README.md with link to the new algorithm
    • Detailed docstrings explaining key concepts and complexity
    • Step-by-step algorithm breakdown with examples

Algorithm Details

  • Time Complexity: O(n) - Each character examined at most twice
  • Space Complexity: O(n) - For transformed string and radius array
  • Reference: "A New Linear-Time On-Line Algorithm for Finding the Smallest Initial Palindrome of a String" by Glenn Manacher (1975), Journal of the ACM

Testing

All tests pass successfully:

  • ✅ 28 new Manacher's Algorithm tests
  • ✅ 12,252 total repository tests passing
  • ✅ 0 warnings, 0 errors
  • ✅ StyleCop compliance maintained

Checklist

  • I have performed a self-review of my code
  • My code follows the style guidelines of this project
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Comments in areas I changed are up to date
  • I have added comments to hard-to-understand areas of my code
  • I have made corresponding changes to the README.md

Fixes

Closes #465
Closes #549

@codomposer codomposer requested a review from siriak as a code owner November 5, 2025 00:30
@codecov
Copy link

codecov bot commented Nov 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.85%. Comparing base (3e4cdb2) to head (d92d7a7).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #554      +/-   ##
==========================================
+ Coverage   96.81%   96.85%   +0.03%     
==========================================
  Files         286      287       +1     
  Lines       11753    11861     +108     
  Branches     1687     1706      +19     
==========================================
+ Hits        11379    11488     +109     
  Misses        237      237              
+ Partials      137      136       -1     

☔ View full report in Codecov by Sentry.
📢 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.

@codomposer
Copy link
Contributor Author

@siriak Could you please take a look this PR?

@codomposer codomposer closed this Nov 6, 2025
@codomposer codomposer reopened this Nov 7, 2025
@siriak siriak enabled auto-merge (squash) November 7, 2025 08:05
Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@siriak siriak merged commit ce9915e into TheAlgorithms:master Nov 7, 2025
4 checks passed
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.

Tests in CI fail from time to time Coverage Issue for HashTable and TimSorter Reducing Overall Code Coverage

2 participants