Longest Palindromic Substring

medium
Unit 13 · Heaps & Binary Search0/3 to milestone

Find the longest substring that reads the same forwards and backwards. Expand around centres: every palindrome has a centre that's either a single character (odd length) or between two characters (even length). There are 2n−12n - 1 centres, and expanding each costs up to O(n)O(n), giving O(n2)O(n^2) overall in O(1)O(1) extra space.

Implement longest_palindrome(s), returning the first-occurring longest one on ties ("" for empty input). Then implement count_palindromic_substrings(s): how many substrings (counted by position) are palindromes.

#strings#two-pointers2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Longest Palindromic Substring · TechGradify