Longest Palindromic Substring

Medium#5from the centre outwards

You get a string. Find its longest piece of consecutive characters that reads the same left to right and right to left. Our example: "xabbay" — the answer is "abba".

input
s: a string of letters and digits.
returns
The longest substring of s that reads the same both ways, uppercase and lowercase letters being different. If several are the longest, any one of them.
constraints
  • 1 ≤ s.length ≤ 1000
  • s: English letters and digits
our 16 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string} s
 * @return {string}
 */
function longestPalindrome(s) {

}
Ctrl ↵
format
reset

saved in this browser

16 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more two pointers problems

Pattern “Two pointers” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.