Problem · String

Longest Palindromic Substring

Learn this problem
MediumInMobi logoInMobiFULLTIMEPHONE SCREEN

Problem statement

Given a string text, return its longest contiguous substring that is a palindrome.

If multiple palindromic substrings have the maximum length, return the one with the smallest starting index.

Function

longestPalindromicSubstring(text: String) → String

Examples

Example 1

text = "ababc"return = "aba"

Both aba and bab have length 3. The substring aba starts earlier.

Example 2

text = "cbbd"return = "bb"

The longest palindromic substring is bb.

Constraints

  • 1 <= text.length <= 2000
  • text contains only lowercase English letters.

More InMobi problems

drafts saved locally
public String longestPalindromicSubstring(String text) {
    // write your code here
}
text"ababc"
expected"aba"
checking account