Implement Manacher's Algorithm and Unit Tests for Longest Palindromic Substring #931
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.
PR Description:
This PR introduces the implementation of Manacher's Algorithm, an efficient algorithm to find the longest palindromic substring in linear time O(n). Additionally, the PR includes a set of comprehensive unit tests to verify the correctness and performance of the algorithm under different scenarios.
Manacher's Algorithm:
The algorithm is designed to find the longest palindromic substring by transforming the input string to handle both even-length and odd-length palindromes uniformly. It expands around potential center points and optimizes the search by leveraging symmetry (mirroring) properties of palindromes. The approach ensures a linear time complexity O(n), making it suitable for handling large strings efficiently.
Use Cases of Manacher's Algorithm:
In cryptography, palindromes might be used in certain algorithms. Identifying the longest palindromic substrings in a ciphered message could help analyze the underlying structure or potential weaknesses.
Unit Tests:
Unit tests have been added to cover a wide range of scenarios:
"abba"
and"racecar"
."babad"
can return either"aba"
or"bab"
)."a!b!a"
) are involved.Test Case Adjustment:
"babad"
was adjusted to handle multiple valid outputs. Both"aba"
and"bab"
are correct longest palindromes for this input, so the test now accepts either of them.