Longest Palindromic Substring

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 14. Everything before it counts as cleared.

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