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 exist.

  • Iterate through the string and expand around each character to find palindromes

  • Keep track of the...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Nagarro

Q. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
Q. Explain Kafka and how you would implement it.
Q. Is the directory a file?
Software Developer 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