| Q & A Home |
| Math |
| Science |
| History |
| IT & Web |
| Programming |
| Health |
| Business |
| Arts & Humanities |
| Social Studies |
| Engineering & Technology |
| Arts & Entertainment |
| Humanities |
| Sports |
| Auto |
| Hobbies |
| Books and Literature |
| Electronics |
| Food & Drink |
| Jobs & Education |
| Law & Government |
| Travel & Places |
| People & Society |
| Beauty & Health |
| Animals & Plants |
| Other |
Ahmad Naima 29 Jun, 2024 11:47:56 PM 1 69
Best Answer: Maintain a variable ‘ maxLength = 1 ‘ (for storing LPS length) and ‘ start =0 ‘ (for storing starting index of LPS).
If the idea is very simple, we will traverse through the entire string with i=0 to i<(length of the string).
While traversing, initialize the ‘low‘ and ‘high‘ pointers such that low= i-1 and high= i+1.
Keep incrementing ‘high’ until str[high]==str[i] .
Similarly keep decrementing ‘low’ until str[low]==str[i].
Finally, keep incrementing ‘high’ and decrementing ‘low’ until str[low]==str[high].
Calculate length=high-low-1, if length > maxLength then maxLength = length and start = low+1.
Print the LPS and return maxLength.
Ahmad Naima 29 Jun, 2024 11:47:56 PM