...

/

Longest Palindromic Substring

Longest Palindromic Substring

Given a string, return the longest palindromic substring within it.

Statement

Given a string of characters, find and return the longest palindromicA palindrome is a sequence of characters that reads the same backwards as forwards, for example, racecar. substring within the input string.

Examples

Example 1

Sample input

"bccd"

Expected output

"cc"

Example 2

Sample input

xaabacxcabaaxcabaax

Expected output

xaabacxcabaax

Try it yourself

#include <iostream>
using namespace std;
string LongestPalindromicSubstring(string s) {
// TODO: WRITE - CODE - HERE
return "-1";
}

Solution

There can be multiple palindromes in the input string, but we have to find the longest one.

There are two ways we can check if a string is a palindrome:

  • Start two pointers from each end of the string. Move towards the center while checking that the element
...
Access this course and 1400+ top-rated courses and projects.