Back to the 100
Problem 033Dynamic Programming
Medium

Longest Palindromic Substring

033

Return the longest palindromic substring in s. If multiple answers exist, return any one of them.

EXAMPLES

Example 1
Input
{
  "s": "babad"
}

Output
"bab"

FUNCTION SHAPE

s: stringstring
SOLUTION NOTE

The worst case is when substrings are palindromes: ie. s = 'aaaaaa'...

Note: there is actually an O(n) solution called Manacher's algorithm, but you aren't expected to know it.

Open on LeetCode
00:00
4 local tests readyRun with ⌘/Ctrl + Enter. Your code stays in this browser.

Runs solve(...) locally in a browser worker. SWE Playbook does not submit your code. Only run code you trust; Python code may access the network.