Longest Palindromic Substring

Question (LC.5)

Given a string, find the longest palindromic substring.

Example

I: "babad"
O: "bab" or "aba"
I: "cbbd"
O: "bb"
maintain a global max

for all substring O(n^2)
    check isPalindrome(substring) O(n)

time O(n^3) space O(1)

DP by Substring

We can memoize the inner substring ex. "aba" => "babad"

Code

Time O(n^2) Space O(n^2)

Middle-out Expansion

There are 2n12n - 1 centers in a length nn string. Expanding them out from there.

Time 2n * n = O(n^2) Space O(1)

Last updated