ExamAdda Logo

Longest Palindromic Substring

Easy

Given a string s, return the longest palindromic substring in s.

A palindrome is a string that reads the same forward and backward.

A substring is a contiguous sequence of characters within a string.

If there are multiple answers with the same maximum length, return any one of them.

Example 1

Input

s = "babad"

Output

"bab"

Explanation

"bab" is a palindrome because it reads the same forward and backward:

bab → bab

 

"aba" is also a valid longest palindromic substring, so "aba" would also be accepted.

Example 2

Input

s = "cbbd"

Output

"bb"

Explanation

The substring "bb" is a palindrome:

bb → bb

 

It is the longest palindromic substring in "cbbd".

Constraints

1 <= s.length <= 1000
s consists of only English letters and digits.
The answer must be a substring
so the characters must be contiguous.
If multiple longest palindromic substrings exist
any one is accepted.

Hints:

Hint 1

Every palindrome has a center.

The center can be:

  • One character → odd-length palindrome
  • Two characters → even-length palindrome
Hint 2

Try expanding outward from every possible center.

For example:

b a b
   ↑
center

 

Expand while the characters on both sides are equal.

Author & Technical Reviewer

Chatla Jithin Sri Surya

Technically reviewed by: Chatla Jithin Sri Surya

Content Writer

<p>IIEST Shibpur | SDE | AI, ML &amp; Gen AI | DSA | Building Intelligent, Scalable Software</p>

Published

Nov 2, 2026

Last updated

Nov 2, 2026

Last technically reviewed

Nov 2, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output