Strings: Longest Palindromic Substring

A palindrome reads the same both ways: "racecar". Every palindrome has a centre. Odd ones centre on a character ("racecar"), even ones between two characters ("ab|ba").

Expand around centre: for each of the 2n - 1 centres, step outwards while the characters on both sides match. Keep the longest you find. Each expansion is O(n), so the total is O(n²) time and O(1) extra space.

The brute force checks every substring with s[i:j] == s[i:j][::-1]. That is O(n²) substrings times O(n) per check: O(n³).

The signal: "palindrome" plus "substring". For a palindrome check of a whole string, s == s[::-1] is enough.

A common mistake: forgetting the even centres, so "abba" returns "a".

Exercise: finish longest_palindrome so it returns the longest substring of s that is a palindrome. If there is a tie, return the one that starts first. Press Check to run the tests.