FastPrepLongest Palindromic Substring

Longest Palindromic Substring

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a string s, return its longest contiguous palindromic substring.

When several longest palindromes exist, return the one with the smallest starting index. Return the empty string for empty input.

Function

longestPalindrome(s: String) → String

Examples

Example 1

s = "babad"return = "bab"

bab and aba both have length three; bab starts earlier.

Example 2

s = "cbbd"return = "bb"

The longest palindrome is the middle pair.

Example 3

s = ""return = ""

Empty input returns empty output.

Constraints

  • 0 <= s.length <= 2000.
  • s contains ASCII characters.

More Google problems

See Google hiring insights
public String longestPalindrome(String s) {
    // Write your code here.
}
s"babad"
expected"bab"
Checking account…