FastPrepLongest Contiguous Palindromic String

Longest Contiguous Palindromic String

Indeed logoIndeed● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Return a longest palindromic substring of text. A palindrome reads the same from left to right and from right to left.

For this exercise, assume the selected characters must be contiguous in the input and retain their original order. You cannot skip characters inside the result or rearrange them.

For this exercise, assume the result is the substring itself. If several palindromic substrings have the maximum length, return the one with the earliest starting index. Empty input returns the empty string.

Function

longestPalindrome(text: String) → String

Examples

Example 1

text = "babad"return = "bab"

Both bab and aba have maximum length three; earliest starting index selects bab.

Example 2

text = "cbbd"return = "bb"

The longest contiguous palindrome is the even-length substring bb.

Example 3

text = ""return = ""

Empty input returns the empty string.

Constraints

  • For this exercise, assume 0 <= text.length <= 500.
  • For this exercise, assume every character is a lowercase English letter from a through z.

More Indeed problems

See Indeed hiring insights
public String longestPalindrome(String text) {
    // Write your code here.
    return "";
}
text"babad"
expected"bab"
Checking account…