Advanced topics · Exercise 96 of 100
Longest palindromic substring
A palindromic substring is a contiguous stretch that reads the same forwards and backwards. Try each letter and each gap between letters as a center to cover both odd and even lengths. Expand while the next pair matches, keeping the longest stretch found.
Try it yourself
What is the longest palindromic substring in the word upnoonit?
One-step hint
Try centers on letters and in the gaps between them. Expand outward while the two letters match, keeping track of the longest span.
Worked solution
- Start in the gap between the two o letters. Expanding past oo matches the two n letters, giving noon, and the next pair p and i differs.
- Every other gap has unequal neighbors, and no single letter has matching letters immediately on both sides. The longest stretch is noon.