Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Longest Palindromic Substring
MEDIUM
35 mins
Strings
Dynamic Programming
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Strings
-
-
Dynamic Programming
-
-
Other topics
Problem solved
Badge
Skill meter
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Longest Palindromic Substring

Contributed by
Ratnesh
Medium
yellow-spark
0/80
Avg time to solve 35 mins
Success Rate 78 %
Share
0 upvotes

Problem Statement

You are given a string ‘S’ of length ‘N’.

You must return the longest palindromic substring in ‘S’.

Note: Return any of them in case of multiple substrings with the same length.

Example:

Input: ‘S’ =’badam’

Output: ‘ada’

‘ada’ is the longest palindromic substring, and it can be proved that it is the longest possible palindromic substring.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2
6
adccdb
6
aaabbb
Sample Output 1 :
dccd
aaa
Explanation Of Sample Input 1 :
For test case 1:

‘S’ =’adccdb’

‘dccd’ is the longest palindromic substring, and it can be proved that it is the longest possible palindromic substring.
Hence we return ‘dccd’.

For test case 2:

‘S’ =’aaabbb’

‘aaa’ is the longest palindromic substring, and it can be proved that it is the longest possible palindromic substring.
Hence we return ‘dccd’.
‘Bbb’ is also a valid answer.
Sample Input 2 :
2
5
hello
1
a 
Sample Output 2 :
ll
a
Reset Code
Full screen
Auto
copy-code
Console