FastPrepLongest Palindromic Substring

Longest Palindromic Substring

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

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

A single character is a palindrome. If several longest answers exist, return the leftmost one. Your solution must use linear time.

Function

longestPalindrome(s: String) → String

Examples

Example 1

s = "babad"return = "bab"

Both bab and aba have maximum length three; bab starts first.

Example 2

s = "cbbd"return = "bb"

The longest palindromic substring is bb.

Constraints

  • 1 <= s.length <= 200000
  • s contains lowercase English letters.
  • Your algorithm must run in O(n) time.
  • If several longest palindromic substrings exist, return the leftmost one.

More Bloomberg LP problems

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