Find the Longest Palindromic Substring

Given a string ‘S’ composed of lowercase English letters, your task is to identify the longest palindromic substring within ‘S’.

If there are multiple longest palindromic substrings of the same length, return the rightmost one in the given string.

Example:

Input:
bbbab
Output:
bab
Explanation:

In the string “bbbab”, there are two longest palindromic substrings of equal length: “bbb” and “bab”. The rightmost one, “bab”, should be returned.

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 100

Where ‘N’ is the length of the string.

Note:
A substring is a contiguous sequence of characters within a string. A string is a palindrome if it reads the same forwards and backwards.
Note:
You do not need to print anything; this has been handled. Implement the function to return the desired result.
AnswerBot
4mo

Find the longest palindromic substring in a given string, returning the rightmost one if multiple substrings are of the same length.

  • Use dynamic programming to check for palindromes within the string.

  • S...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at Goldman Sachs

Q. Write the code for Merge Sort.
Q. Given a sorted array of integers nums and an integer target, write a function to...read more
Q. Rectangle Area Problem Statement You are provided with a list of rectangles, eac...read more
Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits