Repository navigation
Fix Manacher's algorithm returning a non-longest palindrome - #15789
Open
NightSeclude wants to merge 1 commit into
Open
NightSeclude wants to merge 1 commit into
NightSeclude wants to merge 1 commit into
Conversation
palindromic_string tracked the best palindrome by its length in the separator-expanded string, where an odd-centered palindrome of length L and an even-centered one of length L + 1 collide on the same value. The strict '<' comparison then kept whichever came first, so inputs such as 'abb' returned 'b' instead of 'bb' and 'caccaaac' returned 'acca' instead of 'caaac'. Track the best palindrome in the coordinates of the original string instead, and guard the empty input that previously raised IndexError. Adds doctests for even-length and mixed cases. Co-Authored-By: Claude Code <[email protected]>
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe your change
Checklist
The bug
strings/manacher.pyclaims to find the longest palindromic substring, but it returns a shorter one whenever the true answer has even length:The existing doctests only use inputs whose longest palindrome is odd-length, which is why the defect went unnoticed.
Root cause
length[j]stores the palindrome length in the separator-expanded string ('a|b|b'), and the code keeps the first center that maximizes it (if max_length < length[j]). Transformed length does not determine original length: for centerjand radiuskthe span[j-k+1, j+k-1]covers only the even indices, so the original length is(2*k-1)//2for an odd center and(2*k-1)//2 + 1for an even one. For'abb'the centersj=2('b', original length 1) andj=3('|', original length 2) both get transformed length 3; the strict<keepsj=2, so the shorter palindrome wins.The fix
Track the best palindrome in the coordinates of the original string — compute the first and last even indices of each center's span, derive the original length from them, and slice
input_stringdirectly. The linear-time inner loop is untouched. An earlyreturn ""handles the empty input that used to raiseIndexError, and doctests for the even-length and mixed cases above were added.Verification
python strings/manacher.py(doctests): passes.ruff check/ruff format --check: clean.pytest tests/: 494 passed.