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

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 24, 2026

Last updated

Aug 24, 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