Problem · String

Longest Palindromic Substring

Learn this problem
MediumByteDance logoByteDanceFULLTIMEPHONE SCREEN

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 ByteDance problems

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