FastPrepLongest Palindromic Substring

Longest Palindromic Substring

IBM logoIBM● MediumFULLTIMEPHONE SCREEN

Problem statement

Given a string s, return its longest palindromic substring. A substring is a contiguous part of the string, and a palindrome reads the same from left to right and right to left.

If several longest palindromic substrings exist, return the one with the smallest starting index. Compare characters exactly, so uppercase and lowercase letters are different.

Function

longestPalindrome(s: String) → String

Examples

Example 1

s = "babad"return = "bab"

Both bab and aba have length 3. The substring bab starts at index 0, earlier than aba at index 1.

Example 2

s = "cbbd"return = "bb"

The two middle characters form bb, a palindrome of length 2. No longer substring is a palindrome.

Constraints

  • 1 <= s.length <= 1000.
  • s contains only ASCII English letters and digits.

More IBM problems

See IBM hiring insights
Saved
public String longestPalindrome(String s) {
    // write your code here
}
s"babad"
expected"bab"
Checking account…