Skip to main content
mediumBlind 75

Longest Palindromic Substring

Given a string, find the longest palindromic substring A palindrome reads the same forward and backward Example: "babad" => "bab" or "aba"

Given a string s, return a longest contiguous substring that reads the same forward and backward. If several substrings share the maximum length, return any one of them; for example, "babad" may return "bab" or "aba". Return the empty string when s is empty.

Examples

You write

longest_palindrome(s: str)

  1. Example 1

    Input
    s = "babad"
    returns
    "bab" or "aba"
  2. Example 2

    Input
    s = "cbbd"
    returns
    "bb"

The pattern behind it

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Two Pointers

I reach for it when

  • The array is sorted, or you can sort it.
  • You need a pair, a triple or a palindrome.

Say this before you type. Say what each pointer means and what a move to the left or right rules out.

All 26 Two Pointers problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Find Longest Palindromic Substringmedium

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.