Facebook PixelLongest Palindromic Substring — Coding Practice
Longest Palindromic SubstringMedium

Longest Palindromic Substring

Medium 30.2k34% acceptance
Two PointersStringDynamic Programming

Given a string s, return the longest contiguous substring of s that is a palindrome.

If two or more palindromic substrings share the maximum length, return the one with the smallest starting index.

Example 1
Input: s = "babad"
Output: "bab"
Both "bab" and "aba" are valid longest palindromes; "bab" wins because it starts earlier.
Example 2
Input: s = "cbbd"
Output: "bb"
The longest palindromic substring is "bb".
Example 3
Input: s = "a"
Output: "a"
A single character is trivially a palindrome.
Constraints
  • 1 ≤ s.length ≤ 1000
  • `s` consists of digits and English letters.
Asked atAmazonMicrosoftGoogleMetaAdobe
JavaScript
Loading editor…
Case 1
"babad"
expected: "bab"
Case 2
"cbbd"
expected: "bb"
Case 3
"a"
expected: "a"
Case 4
"ac"
expected: "a"
Case 5
"forgeeksskeegfor"
expected: "geeksskeeg"
Case 6
"bb"
expected: "bb"
Case 7
"abacdfgdcaba"
expected: "aba"