If at all we need a comparison, we will only compare actual characters, which are at “odd” positions like 1, 3, 5, 7, etc.
Even positions do not represent a character in string, so no comparison will be preformed for even positions.
If two characters at different odd positions match, then they will increase LPS length by 2.
There are many ways to implement this depending on how even and odd positions are handled. One way would be to create a new string 1st where we insert some unique character (say #, $ etc) in all even positions and then run algorithm on that (to avoid different way of even and odd position handling). Other way could be to work on given string itself but here even and odd positions should be handled appropriately.
Here we will start with given string itself. When there is a need of expansion and character comparison required, we will expand in left and right positions one by one. When odd position is found, comparison will be done and LPS Length will be incremented by ONE. When even position is found, no comparison done and LPS Length will be incremented by ONE (So overall, one odd and one even positions on both left and right side will increase LPS Length by TWO).
LPS of string is babcbabcbaccba : abcbabcba LPS of string is abaaba : abaaba LPS of string is abababa : abababa LPS of string is abcbabcbabcba : abcbabcbabcba LPS of string is forgeeksskeegfor : geeksskeeg LPS of string is caba : aba LPS of string is abacdfgdcaba : aba LPS of string is abacdfgdcabba : abba LPS of string is abacdedcaba : abacdedcaba
This article is contributed by Anurag Singh. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Z algorithm (Linear time pattern searching Algorithm)
- Longest Palindromic Substring | Set 2
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 1
- Longest palindromic string possible after removal of a substring
- Suffix Tree Application 6 - Longest Palindromic Substring
- Minimum length of substring whose rotation generates a palindromic substring
- Shortest Palindromic Substring
- Find the time which is palindromic and comes after the given time
- Check if a substring can be Palindromic by replacing K characters for Q queries
- Maximum length palindromic substring such that it starts and ends with given char
- Longest Palindromic Subsequence | DP-12
- Print Longest Palindromic Subsequence
- Length of longest palindromic sub-string : Recursion
- Longest Palindromic Subsequence of two distinct characters
- Print the longest palindromic prefix of a given string
Improved By : DmitriyPhilimonov