Problem · String

Longest Palindromic Substring

Learn this problem
MediumeBay logoeBayFULLTIMEPHONE SCREEN

Problem statement

Given a string s, return its longest contiguous substring that reads the same from left to right and from right to left.

If more than one longest palindromic substring exists, return the one whose first character appears earliest in s.

Function

longestPalindrome(s: String) → String

Examples

Example 1

s = "babad"return = "bab"

Both bab and aba have maximum length three. The earlier one is bab.

Example 2

s = "cbbd"return = "bb"

The two middle characters form the unique longest palindrome.

Example 3

s = "forgeeksskeegfor"return = "geeksskeeg"

The ten-character middle substring is palindromic and no longer substring is.

Constraints

  • 1 <= s.length <= 2000.
  • s contains only uppercase English letters, lowercase English letters, and digits.
  • Character comparisons are case-sensitive.

More eBay problems

drafts saved locally
public String longestPalindrome(String s) {
    // Write your code here.
}
s"babad"
expected"bab"
checking account