Quant Memo
Coding/●●●●

Longest palindromic substring

Given a string, return the longest contiguous substring that reads the same forwards and backwards.

"babad"   -> "bab"      # "aba" is equally valid
"cbbd"    -> "bb"
"a"       -> "a"

Return any one longest palindromic substring. The brute force, check all O(n2)O(n^2) substrings at O(n)O(n) each, is O(n3)O(n^3); aim for O(n2)O(n^2).

Show a hint

Instead of asking "is this substring a palindrome?", fix the center of a candidate palindrome and grow outward. How many possible centers are there?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions